JQ 实现下拉选择框

html

<div id="dd" class="wrapper-dropdown-3" tabindex="1">
                        <span>Transportspan>
                        <ul class="dropdown">
                            <li><a href="#"><i class="icon-envelope ">i>Classic maila>li>
                            <li><a href="#"><i class="icon-truck ">i>UPS Deliverya>li>
                            <li><a href="#"><i class="icon-plane ">i>Private jeta>li>
                        ul>
div>

css

.wrapper-dropdown-3 {
    /* Size and position */
    position: relative;
    width: 250px;
    margin: 0 auto;
    padding: 10px;

    /* Styles */
    background: #fff;
    border-radius: 7px;
    border: 1px solid rgba(0,0,0,0.15);
    box-shadow: 0 1px 1px rgba(50,50,50,0.1);

}

.wrapper-dropdown-3 .dropdown {
  /* Size & position */
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;

    font-weight: normal;    
    /* Hiding */
    opacity: 0;
    pointer-events: none;    
}

.wrapper-dropdown-3 .dropdown li a {
    display: block;
    padding: 10px;
    text-decoration: none;
    color: #8aa8bd;
    border-bottom: 1px solid #e6e8ea;
}

/* Active state */

.wrapper-dropdown-3.active .dropdown {
    opacity: 1;
    pointer-events: auto;
}

JS

function DropDown(el) {
                this.dd = el;
                this.placeholder = this.dd.children('span');
                this.opts = this.dd.find('ul.dropdown > li');
                this.val = '';
                this.index = -1;
                this.initEvents();
            }
            DropDown.prototype = {
                initEvents : function() {
                    var obj = this;
                    obj.dd.on('click', function(event){
                        $(this).toggleClass('active');
                        return false;
                    });

                    obj.opts.on('click',function(){
                        var opt = $(this);
                        obj.val = opt.text();
                        obj.index = opt.index();
                        obj.placeholder.text(obj.val);
                    });
                },
                getValue : function() {
                    return this.val;
                },
                getIndex : function() {
                    return this.index;
                }
            }

            $(function() {

                var dd = new DropDown( $('#dd') );

                $(document).click(function() {
                    // all dropdowns
                    $('.wrapper-dropdown-3').removeClass('active');
                });

            });

你可能感兴趣的:(H5)