目錄
回复讨论(解决方案)
首頁 web前端 html教學 JS+CSS 做下拉菜单背景问题_html/css_WEB-ITnose

JS+CSS 做下拉菜单背景问题_html/css_WEB-ITnose

Jun 24, 2016 pm 12:22 PM

select 滚动条


下面的代码是改变下拉菜单样式的,可以正常使用,但是现在有个问题就是下拉的内容很多,我想到一定高度就自动有滚动条,麻烦哪位兄弟帮我看看,感觉感谢。

<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>
登入後複製


<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>
登入後複製

<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> 
登入後複製


回复讨论(解决方案)

默认overflow是visible,会自动撑开,给#macstyle1 ul.tag_options一个合适的高度并让overflow-y:auto;就行了,为了美观overflow-x可以设置为hidden。出来滚动条之后宽度有点不够,建议加一点。

<!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>
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

&gt; gt;的目的是什麼 元素? &gt; gt;的目的是什麼 元素? Mar 21, 2025 pm 12:34 PM

本文討論了HTML&lt; Progress&gt;元素,其目的,樣式和與&lt; meter&gt;元素。主要重點是使用&lt; progress&gt;為了完成任務和LT;儀表&gt;對於stati

&lt; datalist&gt;的目的是什麼。 元素? &lt; datalist&gt;的目的是什麼。 元素? Mar 21, 2025 pm 12:33 PM

本文討論了html&lt; datalist&gt;元素,通過提供自動完整建議,改善用戶體驗並減少錯誤來增強表格。Character計數:159

HTML5中跨瀏覽器兼容性的最佳實踐是什麼? HTML5中跨瀏覽器兼容性的最佳實踐是什麼? Mar 17, 2025 pm 12:20 PM

文章討論了確保HTML5跨瀏覽器兼容性的最佳實踐,重點是特徵檢測,進行性增強和測試方法。

&lt; meter&gt;的目的是什麼。 元素? &lt; meter&gt;的目的是什麼。 元素? Mar 21, 2025 pm 12:35 PM

本文討論了HTML&lt; meter&gt;元素,用於在一個範圍內顯示標量或分數值及其在Web開發中的常見應用。它區分了&lt; meter&gt;從&lt; progress&gt;和前

如何使用HTML5表單驗證屬性來驗證用戶輸入? 如何使用HTML5表單驗證屬性來驗證用戶輸入? Mar 17, 2025 pm 12:27 PM

本文討論了使用HTML5表單驗證屬性,例如必需的,圖案,最小,最大和長度限制,以直接在瀏覽器中驗證用戶輸入。

視口元標籤是什麼?為什麼對響應式設計很重要? 視口元標籤是什麼?為什麼對響應式設計很重要? Mar 20, 2025 pm 05:56 PM

本文討論了視口元標籤,這對於移動設備上的響應式Web設計至關重要。它解釋瞭如何正確使用確保最佳的內容縮放和用戶交互,而濫用可能會導致設計和可訪問性問題。

&lt; iframe&gt;的目的是什麼。 標籤?使用時的安全考慮是什麼? &lt; iframe&gt;的目的是什麼。 標籤?使用時的安全考慮是什麼? Mar 20, 2025 pm 06:05 PM

本文討論了&lt; iframe&gt;將外部內容嵌入網頁,其常見用途,安全風險以及諸如對象標籤和API等替代方案的目的。

Gitee Pages靜態網站部署失敗:單個文件404錯誤如何排查和解決? Gitee Pages靜態網站部署失敗:單個文件404錯誤如何排查和解決? Apr 04, 2025 pm 11:54 PM

GiteePages靜態網站部署失敗:404錯誤排查與解決在使用Gitee...

See all articles