欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ui組件之input多選下拉實(shí)現(xiàn)方法(帶有搜索功能)

 更新時(shí)間:2016年07月14日 09:38:26   作者:裕軒汪  
這篇文章主要介紹了ui組件之input多選下拉實(shí)現(xiàn)方法(帶有搜索功能)的相關(guān)資料,需要的朋友可以參考下

我的風(fēng)格,先上效果圖,如果大家感覺(jué)還不錯(cuò),請(qǐng)參考實(shí)現(xiàn)代碼。

這里寫(xiě)圖片描述

這里寫(xiě)圖片描述

這里寫(xiě)圖片描述

廢話不說(shuō) 先看div層次結(jié)構(gòu)

<!-- 最外層div 可以任意指定 主要用于定義子元素寬度 -->
<div class="col-xs-10" style="width:800px">
<!-- 表單label 添加文字提示 -->
<label for="" class="label-control">全文檢索</label>
<!-- 多選承接div 以后會(huì)動(dòng)態(tài)添加span -->
<div class="hint-input-span-container">
<!-- 表單元素 用來(lái)綁定監(jiān)聽(tīng)事件以及接收用戶(hù)輸入 該層上方會(huì)動(dòng)態(tài)添加span -->
<input type="text" name="hint-search" value="" placeholder="選定關(guān)鍵字或按下tab或按下enter來(lái)分割關(guān)鍵字">
</div>
<!-- 包含下拉列表列 -->
<div class="hint-block">
<!-- 根據(jù)json數(shù)據(jù)包動(dòng)態(tài)添加li -->
<ul class="hint-ul">
</ul>
</div>
</div>

dom結(jié)構(gòu)注釋已經(jīng)能說(shuō)得清楚了,下面來(lái)看css

* {
box-sizing: border-box;
}
.hint-input-span-container {
width:100%;
background-color: #fff;
border: 1px solid #ccc;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
display: inline-block;
padding: 2px 4px;
color: #555;
vertical-align: middle;
border-radius: 1px;
max-width: 100%;
line-height: 30px;
}
.hint-input-span-container .tag {
padding: -2px;
font-size: 12px;
font-family: serif;;
margin-right: 2px;
margin-top: 2px;
margin-bottom: 2px;
display: inline-block;
}
.label {
font-size: 10px;
padding: 4px 6px;
border: none;
text-shadow: none;
border-radius: 3px;
font-weight: 200;
}
.label-primary {
background: #2693FF;
color: white;
}
.hint-input-span-container span i[data-role='remove'] {
cursor: pointer;
}
.tag {
margin-right: 2px;
color: white;
}
.tag [data-role="remove"] {
margin-left: 2px;
cursor: pointer;
}
input[name='hint-search'] {
border: none;
box-shadow: none;
outline: none;
background-color: transparent;
padding: 0;
margin: 0;
width: 100%;
max-width: inherit;
}
.hint-block {
position: absolute;
width: 100px;
max-height: 120px;
background-color: #fff;
overflow: auto;
display: none;
z-index: 9999;
}
.hint-ul {
text-decoration: none;
list-style-type: none;
padding-left: 5px;
}
.hint-ul li{
font-size: 14px;
padding: 2px 4px;
}
.hint-ul li:hover{
background-color: #eee;
}

css中設(shè)置border-sizing:border-box很重要,這個(gè)屬性可以使padding與border計(jì)算在width之中

.hint-input-span-container 設(shè)置display為inline-block也很重要,有關(guān)于tag的排列

.hint-block設(shè)置z-index為9999保證顯示在最前端,同時(shí)position為absolute保證其位置

其他的都可以根據(jù)自己需要調(diào)整

下面來(lái)看js代碼

$(function(){
//json數(shù)據(jù)包
var data = {data:["123","北京你好","北京歡迎您","北京好","海洋","海洋廣利局","我海洋","我吃驚","我啦啦啦啦","我不能忍","機(jī)構(gòu)","日本","俄羅斯的山","埃塞俄比亞","伊巴卡","比比比"]};
//獲取后面需要多次調(diào)用的dom對(duì)象
var $hintSearch = $("input[name='hint-search']");
var $hintSearchContainer = $(".hint-input-span-container");
var $hintBlock = $(".hint-block");
var $hintUl = $(".hint-ul");
//初次調(diào)用添加詞典
addDictionary(data.data,addUlListener);
//設(shè)置詞典列表寬度
setHintSearchContainerWidth();
//實(shí)現(xiàn)響應(yīng)式 監(jiān)聽(tīng)resize事件
$(window).bind('resize', setHintSearchContainerWidth);
//獲得焦點(diǎn)
$hintSearch.focus(function(){
animteDown();
});
//失去焦點(diǎn)
//設(shè)置延遲為了可以監(jiān)聽(tīng)到click的響應(yīng)
$hintSearch.blur(function(){
setTimeout(function(){
animateUp();
},200);
});
//TAB 與 enter事件
//監(jiān)聽(tīng)tab與enter兩個(gè)鍵位 如果input內(nèi)有輸入的內(nèi)容,則添加span
//注意最后要阻止一下事件冒泡 防止跳轉(zhuǎn)與切換焦點(diǎn)
$hintSearch.keydown(function(e){
switch (e.which) {
case 9: case 13:{
var text = $hintSearch.val();
if(!$.trim(text)) {
$hintSearch.val("");
e.preventDefault();
return;
}
if( !checkContainerHas(text) ) {
$hintSearch.before('<span class="tag label label-primary">'+ text +' <i class="fa fa-times" data-role="remove"></i><i>&nbsp;</i></span>');
addSpanListenr();
}
//console.log($hintSearch.val());
$hintSearch.val("");
$hintSearch.focus();
e.preventDefault();
break;
}
default: ;
}
});
//檢測(cè)輸入配對(duì)
//對(duì)輸入內(nèi)容在li中進(jìn)行匹配 如果包含字符串可以找到并返回
//搜索方法可以自行修改,只要保證返回一個(gè)搜索后的數(shù)組即可
$hintSearch.keyup(function(e){
var text = $hintSearch.val();
if (!$.trim(text)){
updateDictionary(data.data,addUlListener);
}
var tmparr = data.data.filter(function(x){
return x.indexOf(text) != -1;
})
if (tmparr.length === 0) {
tmparr.push("無(wú)匹配條目");
}
updateDictionary(tmparr,addUlListener);
})
//函數(shù)庫(kù)
//添加用戶(hù)常用字典庫(kù)
function addDictionary(dataarr, callback) {
for(var i = 0; i < dataarr.length; i++) {
$hintUl.append('<li>'+ dataarr[i] +'</li>');
}
callback();
}
//更新搜索內(nèi)容
function updateDictionary(dataarr,callback) {
$hintUl.empty();
addDictionary(dataarr,callback);
}
//向下滑動(dòng)動(dòng)畫(huà)
//封裝改變樣式邊框
function animteDown()
{
$hintBlock.slideDown('fast').css({'border':'1px solid #96C8DA','border-top' : '0px', 'box-shadow' : '0 2px 3px 0 rgba(34,36,38,.15)'});
$hintSearchContainer.css({'border':'1px solid #96C8DA','border-bottom' : '0px', 'box-shadow' : '0 2px 3px 0 rgba(34,36,38,.15)'});
}
//向上滑動(dòng)動(dòng)畫(huà)
function animateUp()
{
$hintBlock.slideUp('fast',function(){
$hintSearchContainer.css({'border':'1px solid #ccc'});
});
}
//檢驗(yàn)是否與輸入的重復(fù)
function checkContainerHas(text)
{
var flag = 0;
$(".hint-input-span-container span").each(function(){
if ($.trim(text) == $.trim($(this).text())) {
flag = 1;
return;
}
});
return flag ? true : false;
}
//設(shè)置hint-input-span-container寬度
function setHintSearchContainerWidth()
{
var hint_width = $hintSearchContainer.width() + 2 * parseInt($hintSearchContainer.css('padding-left').match(/[0-9]+/)[0]) + 2 * parseInt($hintSearchContainer.css('border-left').match(/[0-9]+/)[0]) ;
$hintBlock.css({'width': hint_width});
}
//綁定click事件
function addUlListener() {
$hintUl.delegate('li','click',function(){
var text = $(this).text();
if(!checkContainerHas(text)) {
$hintSearch.before('<span class="tag label label-primary">'+ text +' <i class="fa fa-times" data-role="remove"></i><i>&nbsp;</i></span>');
addSpanListenr();
}
$hintSearch.val("");
animateUp();
})
}
//監(jiān)聽(tīng) span事件
function addSpanListenr() {
$(".hint-input-span-container span").delegate("i",'click',function(){
$(this).parent().remove();
})
}
})

重點(diǎn)就是對(duì)事件的監(jiān)聽(tīng)以及dom元素的操作,要依賴(lài)于jquery。

以上所述是小編給大家介紹的ui組件之input多選下拉實(shí)現(xiàn)方法(帶有搜索功能),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論