Home  >  Article  >  Web Front-end  >  Based on jquery, the beautiful drop-down box can be modified twice_jquery

Based on jquery, the beautiful drop-down box can be modified twice_jquery

WBOY
WBOYOriginal
2016-05-16 17:14:101049browse

Continue to post an article about the web front-end custom control - ComboBox (drop-down box). In the past, when I used the drop-down box control, I was always troubled by the ugly style. Now I share this control. I hope that useful colleagues can collect it or use it again. Modify to achieve the effect you want.

Decompose the custom drop-down box:

1. Create a constructor and initialize the assigned control value.

2. The bound control is presented in the foreground.

3. Click the drop-down box control to display the drop-down list

4. Click to trigger the drop-down box control and collapse the drop-down list.

5. Click the drop-down item to trigger the event.

The code is as follows:

Html code:

Copy code The code is as follows:



css style:
Copy code The code is as follows:

.dropdown span a{float:left;background:url(/img/Icon_BG.png);}
/*Drop-down box http://power.76741.com*/
.dropdown span a{background-position: -213px -75px;}
.dropdown{float:left;width:105px;}
.dropdown span{border:solid 1px #ccc;width:95%;height:28px;background:url(/img/tbline_bg.png);border-radius:8px;overflow:hidden;}
. dropdown span{float:left;padding-left:10px;line-height:28px; cursor:pointer;}
.dropdown span.active{border-radius:8px 8px 0px 0px;}
.dropdown span font {width:auto;margin-right: 0px;float:left;}
.dropdown span a{float:right;width:20px;height:20px;margin:4px 0;}
.dropdown p{border :solid 1px #ccc;border-top:0px;width:103px;display:none;position:absolute;margin-top:28px;background-color:#fff;z-index:3;max-height:280px;overflow -y: auto; overflow-x: hidden;}
.dropdown p a{float:left;line-height:28px;height:28px;padding-left:10px;color:#666;font-size:14px; cursor:default;text-align:left;width:100%;overflow:hidden;}
.dropdown p a:hover{background:url(/img/tbline_bg.png);color:#666;}

Js code:

1. Custom class:
Copy code The code is as follows:

//下拉框
var ComboBox = function () {
this.tag;
this.data_default;
this.data_list;
this.index = 0;

var _this = this;
var _index, _tag, _value;
//初始化
this.init = function () {
_tag = _this.tag;
_index = _this.index;
//设置对象
_this.setDropdown(_this.data_default, _this.data_list);
//赋值绑定事件
if (_tag.find('span font').length > 0) _value = _tag.find('span font').attr('_id');
if (_tag == undefined) { return false; }
_this.showEvent();
_this.selectedIndex(_index);
return true;
}
//设置下拉列表
this.setDropdown = function (default_data, list) {
var css = _tag.attr('class');
if (default_data == undefined) {
default_data = { id: 'null', name: '' };
}
var _html = '';
if (_tag.find('p').length > 0 && _tag.find('span').length > 0) {
$.each(list, function (i, value) {
_html = '' value.name '';
});
_tag.find('span font').replaceWith('' default_data.name '');
_tag.find('p').html(_html);
} else {
_html = '';
var parent = _tag.parent();
_tag.replaceWith(_html);
_tag = parent.find('.dropdown' (css.length > 0 ? '.' css.replace(' ', '.') : ''));
}
}
//下拉事件
this.showEvent = function () {
_tag.find('span').unbind('click').click(function () {
var p = $(this).parent().find('p');
if (p.css('display') == 'block') {
p.css('display', 'none');
$(this).removeClass('active');
} else if (p.html().length > 0) {
p.css('display', 'block');
$(this).addClass('active');
}
});
}
//选中事件
this.selectedIndex = function (index) {
_tag.find('p a').unbind('click').click(function () {
var parent = $(this).parent().parent();
//给下拉框赋值
if ($(this).text().length > 0) {
var font = parent.find('font');
font.text($(this).text());
font.attr("_id", $(this).attr('_id'));
_this.selectedIndexExpand(parent, $(this).index());
parent.find('span').removeClass('active');
}
parent.find('p').css('display', 'none');
});
if (_tag.find('p a').length <= _index) _index = 0;
if (_value && _value != '') {
_index = _tag.find('p a[_id="' _value '"]').index();
}
_tag.find('p a:eq(' _index ')').click();
}
//选中事件扩展
this.selectedIndexExpand = function (tag, index) { }
}

2、示例代码:
复制代码 代码如下:

//http://www.naoqiu.com
var array_state = [{ id: -1, name: '状态' }, { id: 1, name: '未成功' }, { id: 2, name: '成功' }, { id: 3, name: '失败'}];
//状态下拉控件
var select_type = new ComboBox();
select_type.tag = $('.select_type');
select_type.data_default = array_state[0];
select_type.data_list = array_state;
select_type.selectedIndexExpand = function (tag, index) {
//fun_Pager();
}
select_type.init();

3、示例图:
Based on jquery, the beautiful drop-down box can be modified twice_jquery
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