Home  >  Article  >  Web Front-end  >  Briefly introduce the sample code sharing of CSS custom drop-down list style

Briefly introduce the sample code sharing of CSS custom drop-down list style

黄舟
黄舟Original
2017-07-26 09:56:113955browse

Default style of drop-down list:


##Introduced below Two methods to customize the drop-down list:

Method 1:

Use pure CSS to customize the style of the drop-down list.

Principle: Clear the default drop-down list style, customize the style, and attach a small arrow picture aligned to the right.

<!doctype html>
<html>
	<head>
		<style type="text/css">
			select{
				width:200px;
				height:30px;
				appearance:none;
			    -moz-appearance:none;
			    -webkit-appearance:none;
				background: url("images/select.png") no-repeat right center;
				font-size:16px;
				font-family:Microsoft YaHei;
				color:red;
			}
		</style>
	</head>
	<body>
		<form action="" method="post">
			<select>
				<option value="请选择">请选择</option>
				<option value="北京">北京</option>
				<option value="上海">上海</option>
				<option value="广州">广州</option>
			</select>
		</form>
	</body>
</html>


Problem: Modifying the width and height of option is invalid.

Method 2:

Use p+ul+jQuery to implement custom style drop-down list selection.

##HTML code:

<p id="container">
	<form action="" method="post">
		<p>
			<ul>
				<li class="active">请选择</li>
				<li>北京</li>
				<li>上海</li>
				<li>广州</li>
			</ul>
		</p>
	</form>
</p>

CSS code:

#container{
	background:grey;
	width:300px;
	height:200px;
	padding:20px;
}
form p{
    width:236px;
    height:34px;
}
form p{
	font-family:Microsoft YaHei;
    background:#FFFFFF;
}
form p:hover{
    border:1px solid #E74F4D;
}
form ul{
	margin:0;
	padding:0;
}
form ul li:first-child{
    height:34px;
    line-height:34px;
}
form ul li{
    width:236px;
    height:24px;
    line-height:24px;
    font-size:15px;
    color:#323333;
    opacity:0.7;
    background:#e3e3e5;
    text-indent:12px;
	display:none;
}
form ul li.active{
    display:block;
    background:url("images/arrows_active_down.gif") no-repeat scroll right center;
    opacity:1;
}
form ul li:not(.active):hover{
    background:#E74F4D;
    color:white;
}

jQuery code:

$(document).ready(function(){
    var p = $("form").find("p");
    p.mouseover(function(e) {
        var event = e || window.event;
        var target = event.target || event.srcElement;
        var _this = $(this);
        if(target.nodeName.toLowerCase() == &#39;li&#39;) {
            _this.find(&#39;li&#39;).css(&#39;display&#39;, &#39;block&#39;);
            _this.find(&#39;li&#39;).click(function(){
                var li = $(this);
                _this.find(&#39;.active&#39;).text(li.text());
            });
        }
        _this.mouseout(function(e) {
            var event = e || window.event;
            var target = event.target || event.srcElement;
            if(target.nodeName.toLowerCase() == &#39;li&#39;)
				_this.find(&#39;li&#39;).not(&#39;.active&#39;).css(&#39;display&#39;,&#39;none&#39;);
        });
    });
});

The above is the detailed content of Briefly introduce the sample code sharing of CSS custom drop-down list style. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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