js實(shí)現(xiàn)select選擇框效果及美化
網(wǎng)上有各種各樣的關(guān)于 select 選擇框的美化,找了很多,并沒有好的樣式效果。所以就找了一個(gè)利用 ul li 做的類似 select 選擇框的效果,不廢話了,先上圖,效果如下:

點(diǎn)擊一個(gè) test ,就會(huì)把列表顯示出來,再次點(diǎn)擊,列表隱藏,選擇一個(gè) li ,就會(huì)把 span 里的內(nèi)容替換成 li 的內(nèi)容,然后可以用 js 監(jiān)控 span 的變化,然后執(zhí)行你的代碼。效果如下:
html 代碼如下:
<div id="type" class="test"> <span>投資種類</span> <ul class="dropdown"> <li>期貨</li> <li>股票</li> <li>期權(quán)</li> </ul> </div> <div id="kind" class="test"> <span>投資類型</span> <ul class="dropdown"> <li>趨勢(shì)</li> <li>震蕩</li> <li>套利</li> <li>選股</li> <li>擇時(shí)</li> </ul> </div>
css 代碼如下:
ul li{
list-style: none;
}
.test {
position: relative;
float: left;
width: 120px;
height: 40px;
padding-left: 11px;
font-size: 15px;
line-height: 40px;
cursor: pointer;
border: 1px solid #d2d2d2;
border-radius: 3px;
margin-right: 20px;
outline: none;
}
.test:before {
position: absolute;
right: 13px;
top: 18px;
width: 0;
height: 0;
content: "";
border-width: 8px 8px 0 8px;
border-style: solid;
border-color: #d36969 transparent;
-webkit-transition: transform .25s;
-moz-transition: transform .25s;
-ms-transition: transform .25s;
-o-transition: transform .25s;
transition: transform .25s;
}
.test:after {
position: absolute;
right: 15px;
top: 18px;
width: 0;
height: 0;
content: "";
border-width: 6px 6px 0 6px;
border-style: solid;
border-color: #fff transparent;
-webkit-transition: all .25s;
-moz-transition: all .25s;
-ms-transition: all .25s;
-o-transition: all .25s;
transition: all .25s;
}
.test.active:before{
-webkit-transform: rotate(180deg);
-moz-transform: rotate(180deg);
-ms-transform: rotate(180deg);
-o-transform: rotate(180deg);
transform: rotate(180deg);
}
.test.active:after{
top: 20px;
-webkit-transform: rotate(180deg);
-moz-transform: rotate(180deg);
-ms-transform: rotate(180deg);
-o-transform: rotate(180deg);
transform: rotate(180deg);
}
.test .dropdown {
position: absolute;
right: 0;
left: 0;
display: none;
padding: 0;
border-radius: inherit;
border: 1px solid #d2d2d2;
box-shadow: 2px 2px 5px rgba(0,0,0,.4);
}
.test.active .dropdown {
display: block;
}
.test .dropdown:before {
position: absolute;
right: 13px;
bottom: 100%;
width: 0;
height: 0;
content: "";
border-width: 0 8px 8px 8px;
border-style: solid;
border-color: #d2d2d2 transparent;
}
.test .dropdown:after {
position: absolute;
right: 15px;
bottom: 100%;
width: 0;
height: 0;
content: "";
border-width: 0 6px 6px 6px;
border-style: solid;
border-color: #fff transparent;
}
.test .dropdown li {
float: left;
width: 129px;
font-size: 14px;
-webkit-transition: all .3s ease-out;
-moz-transition: all .3s ease-out;
-ms-transition: all .3s ease-out;
-o-transition: all .3s ease-out;
transition: all .3s ease-out;
text-align: center;
}
.test .dropdown li:first-of-type {
border-radius: 3px 3px 0 0;
}
.test .dropdown li:last-of-type {
border-radius: 0 0 3px 3px;
}
.test .dropdown li:hover {
color: #fff;
background: #c43c3d;
}
對(duì)于 :before 和 :after 兩個(gè)偽元素不理解可以去看看我上篇博客 點(diǎn)擊這里
js 代碼如下:
function DropDown(el) {
this.dd = el;
this.span = this.dd.children('span');
this.li = this.dd.find('ul.dropdown li');
this.val = '';
}
DropDown.prototype.initEvents = function() {
var obj = this;
obj.dd.on('click', function(event){
$(this).toggleClass('active').siblings().removeClass('active');
event.stopPropagation();
});
obj.li.on('click', function() {
var opt = $(this);
obj.val = opt.html();
if (obj.span.html() == obj.val) return;
obj.span.html(obj.val);
$(document).click(function() {
$('.test').removeClass('active');
});
})
}
var test1 = new DropDown($('#type'));
var test2 = new DropDown($('#kind'));
test1.initEvents();
test2.initEvents()
這里使用構(gòu)造-原型組合模式來創(chuàng)建了一個(gè) DropDown 對(duì)象,構(gòu)造-原型組合模式解釋:屬性寫在構(gòu)造函數(shù)中,是表示每個(gè)實(shí)例獨(dú)有的屬性,讓對(duì)象具體化;方法寫在構(gòu)造函數(shù)外,是為了表示每個(gè)實(shí)例共享的方法。
但是這里有點(diǎn)不好的方法是,已限制了 html 的布局。
精彩專題分享:javascript選擇框操作匯總 jquery選擇框操作匯總
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS簡(jiǎn)單設(shè)置下拉選擇框默認(rèn)值的方法
- js表單處理中單選、多選、選擇框值的獲取及表單的序列化
- js下拉選擇框與輸入框聯(lián)動(dòng)實(shí)現(xiàn)添加選中值到輸入框的方法
- js實(shí)現(xiàn)仿阿里巴巴城市選擇框效果實(shí)例
- JS+CSS實(shí)現(xiàn)實(shí)用的單擊輸入框彈出選擇框的方法
- 自定義的一個(gè)簡(jiǎn)單時(shí)尚js下拉選擇框
- js 自定義個(gè)性下拉選擇框示例
- Js(JavaScript)中,彈出是或否的選擇框示例(confirm用法的實(shí)例分析)
- js實(shí)現(xiàn)一個(gè)省市區(qū)三級(jí)聯(lián)動(dòng)選擇框代碼分享
- js判斷選擇時(shí)間不能小于當(dāng)前時(shí)間的示例代碼
- JS日期和時(shí)間選擇控件升級(jí)版(自寫)
- JS時(shí)間選擇器 兼容IE6,7,8,9
- JS實(shí)現(xiàn)漂亮的時(shí)間選擇框效果
相關(guān)文章
JavaScript實(shí)現(xiàn)在標(biāo)題欄上顯示當(dāng)前日期的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)在標(biāo)題欄上顯示當(dāng)前日期的方法,涉及javascript操作時(shí)間及DOM節(jié)點(diǎn)的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
在js文件中引入(調(diào)用)另一個(gè)js文件的三種方法
這篇文章主要介紹了在js文件中引入(調(diào)用)另一個(gè)js文件的三種方法,幫助大家更好的理解和學(xué)習(xí)JavaScript,感興趣的朋友可以了解下2020-09-09
使用JS實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
這篇文章主要為大家詳細(xì)介紹了使用JS實(shí)現(xiàn)簡(jiǎn)易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
js動(dòng)態(tài)往表格的td中添加圖片并注冊(cè)事件
這篇文章主要介紹了使用js動(dòng)態(tài)往表格的td中添加圖片并注冊(cè)事件,需要的朋友可以參考下2014-06-06
Javascript數(shù)組的排序 sort()方法和reverse()方法
JavaScript提供了sort()方法和reverse()方法,使得我們可以簡(jiǎn)單的對(duì)數(shù)組進(jìn)行排序操作和逆序操作2012-06-06
JS實(shí)現(xiàn)隊(duì)列的先進(jìn)先出功能示例
這篇文章主要介紹了JS實(shí)現(xiàn)隊(duì)列的先進(jìn)先出功能,結(jié)合實(shí)例形式分析了基于JS的隊(duì)列初始化、賦值等相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
微信小程序復(fù)選框?qū)崿F(xiàn)多選一功能過程解析
這篇文章主要介紹了微信小程序復(fù)選框?qū)崿F(xiàn)多選一功能過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02

