Home  >  Article  >  Web Front-end  >  Sample code sharing for div simulation select box

Sample code sharing for div simulation select box

黄舟
黄舟Original
2017-09-17 09:58:221886browse

I’m not very good at summarizing things that were very early on, so I suddenly felt that I had recorded them all, and the small demo was relatively simple. Discussions and corrections are welcome.

The selection box of the previous UI design draft did not want the default style. It was too cumbersome to change the default style, and some could not be changed, so I simply wrote a p-simulated selection box to see the effect first:

There are not many code implementations, and they are very simple. The js part is purely original, so it can be used without referencing other frameworks:

First the HTML part:


<!DOCTYPE html><html>

    <head>
        <meta charset="UTF-8">
        <title>p模拟select选择框</title>
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <link href="css/customSelect.css" rel="stylesheet" />
        
    </head>

    <body>
        
        <p class="custom-select-box">
            <p class="select-box mui-inline" style="width: 220px;">
                <span id="fisrt" data-show="0">1</span>
                <ul class="custom-option-box">
                    <li class="active"><span>1</span></li>
                    <li><span>2</span></li>
                    <li><span>3</span></li>
                    <li><span>4</span></li>
                </ul>
            </p>
                            
        </p>
         
         
        <script src="js/customSelect.js"></script>
        
        
    </body></html>

css part:

ul,li,p,span{
    box-sizing: border-box;
}.select-box ul,
.select-box li {
    list-style-type: none;
    margin: 0;
    padding: 0;
    cursor: pointer;
    background: #fff;
}.select-box {
    width: 100%;
    position: relative;
    background: #fff;
}#fisrt {
    display: block;
    line-height: 40px;
    width: 100%;
    height: 40px;
    padding: 0 15px;
    padding-right: 20px;
    border: 1px solid #ccc;
    border-radius: 3px;    /*white-space: nowrap;*/
    overflow: hidden;
    cursor: pointer;
}#fisrt.active {
    border: 1px solid #F2C051;
}#fisrt::after {
    content: "";
    display: block;
    position: absolute;
    top: 18px;
    right: 10px;
    width: 0;
    height: 0;
    border: 5px solid transparent;
    border-top-color: #000;
    background: #fff;
}.custom-option-box {
    display: none;
    position: absolute;
    left: 0;
    top: 40px;
    background: #fff;
    border: 1px solid #ccc;
    border-top-color: transparent;
    z-index: 10000;
}.custom-option-box li {
    display: block;
    line-height: 30px;
    padding: 0 15px;
    z-index: 10000;
}.custom-option-box li:hover, 
.custom-option-box li.active {
    background: #F2C051;
}.custom-option-box span {
    cursor: pointer;
}

js part: (the js part is a little rough, put it up now, and will modify it in the next two days)


(function() {
    ready(function() {        
    var option_box = document.getElementsByClassName("custom-option-box")[0],
            select_box = document.getElementsByClassName("select-box")[0],
            width;
        option_box.style.display = "none"; //初始ul隐藏  
        width = select_box.offsetWidth; //select的宽度 默认 100%  
        option_box.style.width = width + "px"; //初始ul宽度           
        document.getElementById("fisrt").addEventListener("click", function() {            
        var isShow = this.dataset.show;            
        if(isShow == 0) {                
        this.dataset.show = 1;                
        this.classList.add("active");                
        this.nextElementSibling.style.display = "block"; //找到ul.son_ul显示  
            } else {                
            this.dataset.show = 0;                
            this.classList.remove("active");                
            this.nextElementSibling.style.display = "none"; //找到ul.son_ul显示              
            }
        },false);        
        var option = option_box.getElementsByTagName("li");        
        for(var i = 0; i < option.length; i++){  
            option[i].onclick = function(){  
                var fisrt = this.parentNode.previousElementSibling;                
                var siblings = getSiblings(this);
                fisrt.innerHTML = this.childNodes[0].innerText;
                fisrt.dataset.val = this.dataset.val;                
                this.classList.add("active");                
                for(var i = 0;i<siblings.length;i++){
                    siblings[i].classList.remove("active");
                }                this.parentNode.style.display = "none";
                fisrt.dataset.show = 0;
                fisrt.classList.remove("active");
            }     
          } 
        
    });    
    function ready (fn) {        
    if(document.addEventListener){        //标准浏览器
            document.addEventListener(&#39;DOMContentLoaded&#39;,function(){                //注销时间,避免重复触发
                document.removeEventListener(&#39;DOMContentLoaded&#39;,arguments.callee,false);
                fn();        
                //运行函数
            },false);
        }else if(document.attachEvent){        //IE浏览器
            document.attachEvent(&#39;onreadystatechange&#39;,function(){                
            if(document.readyState==&#39;complete&#39;){
                    document.detachEvent(&#39;onreadystatechange&#39;,arguments.callee);
                    fn();        
                    //函数运行                
                    }
            });
        }
    }    
    function getSiblings (elm) {        
    var a = [];        
    var p = elm.parentNode.children;        
    for(var i = 0, pl = p.length; i < pl; i++) {            
    if(p[i] !== elm) a.push(p[i]);
        }        
        return a;
    }
})();

The above is the detailed content of Sample code sharing for div simulation select box. 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