Table of Contents
Reply to discussion (solution)
Home Web Front-end HTML Tutorial JS CSS drop-down menu background problem_html/css_WEB-ITnose

JS CSS drop-down menu background problem_html/css_WEB-ITnose

Jun 24, 2016 pm 12:22 PM

select scroll bar


The following code changes the style of the drop-down menu and can be used normally. However, there is a problem now that there is a lot of content in the drop-down menu. I think there will be a scroll bar automatically at a certain height. Could anyone help me please, thank you very much.

1

<script language="JavaScript" type="text/javascript">var selects = document.getElementsByTagName('select');var isIE = (document.all && window.ActiveXObject && !window.opera) ? true : false;function $(id) { return document.getElementById(id);}function stopBubbling (ev) {        ev.stopPropagation();}function rSelects() { for (i=0;i<selects.length;i++){      selects[i].style.display = 'none';      select_tag = document.createElement('div');         select_tag.id = 'select_' + selects[i].name;            select_tag.className = 'select_box';        selects[i].parentNode.insertBefore(select_tag,selects[i]);      select_info = document.createElement('div');                select_info.id = 'select_info_' + selects[i].name;          select_info.className='tag_select';         select_info.style.cursor='pointer';     select_tag.appendChild(select_info);        select_ul = document.createElement('ul');               select_ul.id = 'options_' + selects[i].name;            select_ul.className = 'tag_options';            select_ul.style.position='absolute';            select_ul.style.display='none';         select_ul.style.zIndex='999';       select_tag.appendChild(select_ul);      rOptions(i,selects[i].name);                mouseSelects(selects[i].name);      if (isIE){          selects[i].onclick = new Function("clickLabels3('"+selects[i].name+"');window.event.cancelBubble = true;");     }       else if(!isIE){         selects[i].onclick = new Function("clickLabels3('"+selects[i].name+"')");           selects[i].addEventListener("click", stopBubbling, false);      }           }}function rOptions(i, name) {  var options = selects[i].getElementsByTagName('option');    var options_ul = 'options_' + name; for (n=0;n<selects[i].options.length;n++){           option_li = document.createElement('li');           option_li.style.cursor='pointer';           option_li.className='open';     $(options_ul).appendChild(option_li);       option_text = document.createTextNode(selects[i].options[n].text);      option_li.appendChild(option_text);     option_selected = selects[i].options[n].selected;       if(option_selected){            option_li.className='open_selected';            option_li.id='selected_' + name;            $('select_info_' + name).appendChild(document.createTextNode(option_li.innerHTML));     }               option_li.onmouseover = function(){ this.className='open_hover';}       option_li.onmouseout = function(){          if(this.id=='selected_' + name){                this.className='open_selected';         }           else {              this.className='open';          }       }           option_li.onclick = new Function("clickOptions("+i+","+n+",'"+selects[i].name+"')");    }}function mouseSelects(name){  var sincn = 'select_info_' + name;  $(sincn).onmouseover = function(){ if(this.className=='tag_select') this.className='tag_select_hover'; }    $(sincn).onmouseout = function(){ if(this.className=='tag_select_hover') this.className='tag_select'; } if (isIE){      $(sincn).onclick = new Function("clickSelects('"+name+"');window.event.cancelBubble = true;");  }   else if(!isIE){     $(sincn).onclick = new Function("clickSelects('"+name+"');");       $('select_info_' +name).addEventListener("click", stopBubbling, false); }}function clickSelects(name){  var sincn = 'select_info_' + name;  var sinul = 'options_' + name;      for (i=0;i<selects.length;i++){          if(selects[i].name == name){                            if( $(sincn).className =='tag_select_hover'){               $(sincn).className ='tag_select_open';              $(sinul).style.display = '';            }           else if( $(sincn).className =='tag_select_open'){               $(sincn).className = 'tag_select_hover';                $(sinul).style.display = 'none';            }       }       else{           $('select_info_' + selects[i].name).className = 'tag_select';           $('options_' + selects[i].name).style.display = 'none';     }   }}function clickOptions(i, n, name){            var li = $('options_' + name).getElementsByTagName('li');   $('selected_' + name).className='open'; $('selected_' + name).id='';    li[n].id='selected_' + name;    li[n].className='open_hover';   $('select_' + name).removeChild($('select_info_' + name));  select_info = document.createElement('div');        select_info.id = 'select_info_' + name;     select_info.className='tag_select';     select_info.style.cursor='pointer'; $('options_' + name).parentNode.insertBefore(select_info,$('options_' + name)); mouseSelects(name); $('select_info_' + name).appendChild(document.createTextNode(li[n].innerHTML)); $( 'options_' + name ).style.display = 'none' ; $( 'select_info_' + name ).className = 'tag_select';    selects[i].options[n].selected = 'selected';}window.onload = function(e) {  bodyclick = document.getElementsByTagName('body').item(0);  rSelects(); bodyclick.onclick = function(){     for (i=0;i<selects.length;i++){              $('select_info_' + selects[i].name).className = 'tag_select';           $('options_' + selects[i].name).style.display = 'none';     }   }}</script>

Copy after login


1

<style type="text/css">body{margin:0px; padding:0px; font-size:12px; color:#FFFFFB; background:#3d4045;   }div,ul,li{margin:0; padding:0;}#macstyle1 .select_box{height:22px;  width:271px;}#macstyle1 div.tag_select{display:block;color:#fff; text-align:left; padding-left:10px;width:271px;height:22px;background:url(http://img.bbs.csdn.net/upload/201307/03/1372819568_91197.gif) no-repeat;line-height:22px;}#macstyle1 div.tag_select_hover{display:block;color:#fff;text-align:left; padding-left:10px;width:271px;height:22px;background:url(http://img.bbs.csdn.net/upload/201307/03/1372819568_91197.gif) no-repeat;line-height:22px;}#macstyle1 div.tag_select_open{display:block;color:#fff;text-align:left; padding-left:10px;width:271px;height:22px;background:url(http://img.bbs.csdn.net/upload/201307/03/1372819568_91197.gif) no-repeat;line-height:22px;}#macstyle1 ul.tag_options{position:absolute;list-style:none;text-align:left;  }#macstyle1 ul.tag_options li{ background:#414348;text-align:left; padding-left:10px;width:241px;height:15px;line-height:15px;color:#fff;overflow:auto;}#macstyle1 ul.tag_options li.open_hover{color:#fff; background:#6c6f75; }#macstyle1 ul.tag_options li.open_selected{color:#fff; }</style>

Copy after login

1

<div id="macstyle1">            <select >              <option  value="GMT-12:00">GMT-12:00 日界线西</option>              <option  value="GMT-11:00">GMT-11:00 中途岛,萨摩亚群岛</option>              <option  value="GMT-10:00">GMT-10:00 夏威夷</option>              <option  value="GMT-9:00">GMT-9:00 阿拉斯加</option>              <option  value="GMT-8:00">GMT-8:00 太平洋时间(美国和加拿大)</option>              <option  value="GMT-7:00">GMT-7:00 山地时间(美国和加拿大)</option>              <option  value="GMT-6:00">GMT-6:00 中部时间(美国和加拿大)</option>              <option  value="GMT-5:00">GMT-5:00 东部时间(美国和加拿大)</option>              <option  value="GMT-4:30">GMT-4:30 加拉斯加</option>              <option  value="GMT-4:00">GMT-4:00 大西洋时间(加拿大)</option>              <option  value="GMT-3:30">GMT-3:30 纽芬兰</option>              <option  value="GMT-3:00">GMT-3:00 乔治郭、巴西利亚</option>              <option  value="GMT-2:00">GMT-2:00 中大西洋</option>              <option  value="GMT-1:00">GMT-1:00 佛得角群岛、亚速尔群岛</option>              <option  value="GMT-0:00">GMT-0:00 都柏林、爱丁堡、伦敦</option>              <option  value="GMT+1:00">GMT+1:00 阿姆斯特丹、柏林、罗马、巴黎</option>              <option  value="GMT+2:00">GMT+2:00 雅典、耶路撒冷、伊斯坦布尔</option>              <option  value="GMT+3:00">GMT+3:00 巴格达、科威特、莫斯科</option>              <option  value="GMT+3:30">GMT+3:30 德黑兰</option>              <option  value="GMT+4:00">GMT+4:00 高加索标准时间</option>              <option  value="GMT+4:30">GMT+4:30 喀布尔</option>              <option  value="GMT+5:00">GMT+5:00 伊斯兰堡、卡拉奇、塔什干</option>              <option  value="GMT+5:30">GMT+5:30 马德拉斯、孟买、新德里</option>              <option  value="GMT+5:45">GMT+5:45 加德满都</option>              <option  value="GMT+6:00">GMT+6:00 阿拉木图、新西伯利亚、达卡</option>              <option  value="GMT+6:30">GMT+6:30 仰光</option>              <option  value="GMT+7:00">GMT+7:00 曼谷、河内、雅加达</option>              <option  value="GMT+8:00" selected="selected">GMT+8:00 北京、乌鲁木齐、新加坡</option>              <option  value="GMT+9:00">GMT+9:00 汉城、东京、大阪、札幌</option>              <option  value="GMT+9:30" >GMT+9:30 阿德莱德、达尔文</option>              <option  value="GMT+10:00" >GMT+10:00 墨尔本、悉尼、堪培拉</option>              <option  value="GMT+11:00">GMT+11:00 马加丹、索罗门群岛</option>              <option  value="GMT+12:00">GMT+12:00 奥克兰、惠灵顿</option>              <option  value="GMT+13:00">GMT+13:00 努库阿洛法</option>            </select>          </div>

Copy after login


Reply to discussion (solution)

The default overflow is visible. It will open automatically. Just give #macstyle1 ul.tag_options a suitable height and set overflow-y:auto;. For the sake of beauty, overflow-x can be set to hidden. After the scroll bar comes out, the width is a bit insufficient. It is recommended to add a little more.

1

<!doctype html><html><head><meta charset="utf-8" /><style type="text/css">body{margin:0px; padding:0px; font-size:12px; color:#FFFFFB; background:#3d4045;   }div,ul,li{margin:0; padding:0;}#macstyle1 .select_box{height:22px;  width:271px;}#macstyle1 div.tag_select{display:block;color:#fff; text-align:left; padding-left:10px;width:271px;height:22px;background:url(http://img.bbs.csdn.net/upload/201307/03/1372819568_91197.gif) no-repeat;line-height:22px;}#macstyle1 div.tag_select_hover{display:block;color:#fff;text-align:left; padding-left:10px;width:271px;height:22px;background:url(http://img.bbs.csdn.net/upload/201307/03/1372819568_91197.gif) no-repeat;line-height:22px;}#macstyle1 div.tag_select_open{display:block;color:#fff;text-align:left; padding-left:10px;width:271px;height:22px;background:url(http://img.bbs.csdn.net/upload/201307/03/1372819568_91197.gif) no-repeat;line-height:22px;}#macstyle1 ul.tag_options{position:absolute;list-style:none;text-align:left;height:300px;overflow-y:auto;overflow-x:hidden;}#macstyle1 ul.tag_options li{ background:#414348;text-align:left; padding-left:10px;width:241px;height:15px;line-height:15px;color:#fff;overflow:auto;}#macstyle1 ul.tag_options li.open_hover{color:#fff; background:#6c6f75; }#macstyle1 ul.tag_options li.open_selected{color:#fff; }</style><script language="JavaScript" type="text/javascript">var selects = document.getElementsByTagName('select'); var isIE = (document.all && window.ActiveXObject && !window.opera) ? true : false; function $(id) {    return document.getElementById(id);} function stopBubbling (ev) {        ev.stopPropagation();} function rSelects() {    for (i=0;i<selects.length;i++){        selects[i].style.display = 'none';        select_tag = document.createElement('div');            select_tag.id = 'select_' + selects[i].name;            select_tag.className = 'select_box';        selects[i].parentNode.insertBefore(select_tag,selects[i]);         select_info = document.createElement('div');                select_info.id = 'select_info_' + selects[i].name;            select_info.className='tag_select';            select_info.style.cursor='pointer';        select_tag.appendChild(select_info);         select_ul = document.createElement('ul');                select_ul.id = 'options_' + selects[i].name;            select_ul.className = 'tag_options';            select_ul.style.position='absolute';            select_ul.style.display='none';            select_ul.style.zIndex='999';        select_tag.appendChild(select_ul);         rOptions(i,selects[i].name);                 mouseSelects(selects[i].name);         if (isIE){            selects[i].onclick = new Function("clickLabels3('"+selects[i].name+"');window.event.cancelBubble = true;");        }        else if(!isIE){            selects[i].onclick = new Function("clickLabels3('"+selects[i].name+"')");            selects[i].addEventListener("click", stopBubbling, false);        }            }}  function rOptions(i, name) {    var options = selects[i].getElementsByTagName('option');    var options_ul = 'options_' + name;    for (n=0;n<selects[i].options.length;n++){            option_li = document.createElement('li');            option_li.style.cursor='pointer';            option_li.className='open';        $(options_ul).appendChild(option_li);         option_text = document.createTextNode(selects[i].options[n].text);        option_li.appendChild(option_text);         option_selected = selects[i].options[n].selected;         if(option_selected){            option_li.className='open_selected';            option_li.id='selected_' + name;            $('select_info_' + name).appendChild(document.createTextNode(option_li.innerHTML));        }                 option_li.onmouseover = function(){    this.className='open_hover';}        option_li.onmouseout = function(){            if(this.id=='selected_' + name){                this.className='open_selected';            }            else {                this.className='open';            }        }              option_li.onclick = new Function("clickOptions("+i+","+n+",'"+selects[i].name+"')");    }} function mouseSelects(name){    var sincn = 'select_info_' + name;     $(sincn).onmouseover = function(){ if(this.className=='tag_select') this.className='tag_select_hover'; }    $(sincn).onmouseout = function(){ if(this.className=='tag_select_hover') this.className='tag_select'; }     if (isIE){        $(sincn).onclick = new Function("clickSelects('"+name+"');window.event.cancelBubble = true;");    }    else if(!isIE){        $(sincn).onclick = new Function("clickSelects('"+name+"');");        $('select_info_' +name).addEventListener("click", stopBubbling, false);    } } function clickSelects(name){    var sincn = 'select_info_' + name;    var sinul = 'options_' + name;         for (i=0;i<selects.length;i++){            if(selects[i].name == name){                            if( $(sincn).className =='tag_select_hover'){                $(sincn).className ='tag_select_open';                $(sinul).style.display = '';            }            else if( $(sincn).className =='tag_select_open'){                $(sincn).className = 'tag_select_hover';                $(sinul).style.display = 'none';            }        }        else{            $('select_info_' + selects[i].name).className = 'tag_select';            $('options_' + selects[i].name).style.display = 'none';        }    } } function clickOptions(i, n, name){            var li = $('options_' + name).getElementsByTagName('li');     $('selected_' + name).className='open';    $('selected_' + name).id='';    li[n].id='selected_' + name;    li[n].className='open_hover';    $('select_' + name).removeChild($('select_info_' + name));     select_info = document.createElement('div');        select_info.id = 'select_info_' + name;        select_info.className='tag_select';        select_info.style.cursor='pointer';    $('options_' + name).parentNode.insertBefore(select_info,$('options_' + name));     mouseSelects(name);     $('select_info_' + name).appendChild(document.createTextNode(li[n].innerHTML));    $( 'options_' + name ).style.display = 'none' ;    $( 'select_info_' + name ).className = 'tag_select';    selects[i].options[n].selected = 'selected'; } window.onload = function(e) {    bodyclick = document.getElementsByTagName('body').item(0);    rSelects();    bodyclick.onclick = function(){        for (i=0;i<selects.length;i++){                $('select_info_' + selects[i].name).className = 'tag_select';            $('options_' + selects[i].name).style.display = 'none';        }    }}</script></head><body><div id="macstyle1">  <select >     <option  value="GMT-12:00">GMT-12:00 日界线西</option>      <option  value="GMT-11:00">GMT-11:00 中途岛,萨摩亚群岛</option>     <option  value="GMT-10:00">GMT-10:00 夏威夷</option>       <option  value="GMT-9:00">GMT-9:00 阿拉斯加</option>        <option  value="GMT-8:00">GMT-8:00 太平洋时间(美国和加拿大)</option>       <option  value="GMT-7:00">GMT-7:00 山地时间(美国和加拿大)</option>        <option  value="GMT-6:00">GMT-6:00 中部时间(美国和加拿大)</option>        <option  value="GMT-5:00">GMT-5:00 东部时间(美国和加拿大)</option>        <option  value="GMT-4:30">GMT-4:30 加拉斯加</option>        <option  value="GMT-4:00">GMT-4:00 大西洋时间(加拿大)</option>      <option  value="GMT-3:30">GMT-3:30 纽芬兰</option>     <option  value="GMT-3:00">GMT-3:00 乔治郭、巴西利亚</option>        <option  value="GMT-2:00">GMT-2:00 中大西洋</option>        <option  value="GMT-1:00">GMT-1:00 佛得角群岛、亚速尔群岛</option>     <option  value="GMT-0:00">GMT-0:00 都柏林、爱丁堡、伦敦</option>      <option  value="GMT+1:00">GMT+1:00 阿姆斯特丹、柏林、罗马、巴黎</option>      <option  value="GMT+2:00">GMT+2:00 雅典、耶路撒冷、伊斯坦布尔</option>       <option  value="GMT+3:00">GMT+3:00 巴格达、科威特、莫斯科</option>     <option  value="GMT+3:30">GMT+3:30 德黑兰</option>     <option  value="GMT+4:00">GMT+4:00 高加索标准时间</option>     <option  value="GMT+4:30">GMT+4:30 喀布尔</option>     <option  value="GMT+5:00">GMT+5:00 伊斯兰堡、卡拉奇、塔什干</option>        <option  value="GMT+5:30">GMT+5:30 马德拉斯、孟买、新德里</option>     <option  value="GMT+5:45">GMT+5:45 加德满都</option>        <option  value="GMT+6:00">GMT+6:00 阿拉木图、新西伯利亚、达卡</option>       <option  value="GMT+6:30">GMT+6:30 仰光</option>      <option  value="GMT+7:00">GMT+7:00 曼谷、河内、雅加达</option>       <option  value="GMT+8:00" selected="selected">GMT+8:00 北京、乌鲁木齐、新加坡</option>     <option  value="GMT+9:00">GMT+9:00 汉城、东京、大阪、札幌</option>     <option  value="GMT+9:30" >GMT+9:30 阿德莱德、达尔文</option>       <option  value="GMT+10:00" >GMT+10:00 墨尔本、悉尼、堪培拉</option>       <option  value="GMT+11:00">GMT+11:00 马加丹、索罗门群岛</option>     <option  value="GMT+12:00">GMT+12:00 奥克兰、惠灵顿</option>       <option  value="GMT+13:00">GMT+13:00 努库阿洛法</option> </select></div> </body></html>

Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

See all articles