js模仿qq下拉列表

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>原生js模拟qq下拉列表 </title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            li{
                list-style: none;
            }
            #list{
                width: 240px;
                border: 1px solid #666;
                margin: 20px auto;
            }
            #list .lis{
            }
            #list h2{
                height: 30px;
                line-height: 30px;
                overflow: hidden;
                background:red;
                color: #f1f1f1;
                cursor:pointer;
            }
            #list h2.active{
                background: pink;
                height: 30px;
                line-height: 30px;
                overflow: hidden;
                color: #666;
            }
            #list h2.active:before{
                display: block;
                width: 0;
                height: 0;
                border-left: 8px solid transparent;
                border-top: 12px solid #666;
                border-right:8px solid transparent;
                border-bottom:2px solid transparent;
                margin-right: 4px;
                content: '';
                float: left;
                margin-top: 10px;
            }
            #list h2:before{
                display: block;
                width: 0;
                height: 0;
                border-left: 12px solid #f1f1f1;
                border-right: 6px solid transparent;
                border-bottom: 9px solid transparent;
                border-top:9px solid transparent;
                content: '';
                float: left;
                margin-top: 10px;
            }
            #list .list ul{
                display: none;
            }
            #list .list ul li{
                line-height: 24px;
                border-bottom: 1px solid #666;
                text-indent: 15px;
            }
            #list .list ul li.hover{
                background: blue;
                color: #F8F8F8;
            }
            #list .list ul li:first-of-type{
                border-top: 1px solid #ccc;
            }
            #list .list:last-of-type ul li:last-of-type{
                border-bottom:none;
            }
        </style>
        <script>
            window.onload=function(){
                var oUl=document.getElementById('list');
                var aH2=oUl.getElementsByTagName('h2');
                var aUl=oUl.getElementsByTagName('ul');
                var h2Len=aH2.length;
                var aLi=null;
                var arrLi=[];
                var aUlLen=aUl.length;
                for(var i=0;i<h2Len;i++){
                    aH2[i].index=i;
                    aH2[i].onclick=function(){
                        if(this.className=='')
                        {
                            aUl[this.index].style.display='block';
                            this.className='active';
                        }
                        else{
                            aUl[this.index].style.display='none';
                            this.className='';
                        }
                    }
                }
                for(var i=0;i<aUlLen;i++){
                    aLi=aUl[i].getElementsByTagName('li');
                    for(var j=0;j<aLi.length;j++){
                        arrLi.push(aLi[j]);
                    }
                }
                for(var i=0;i<arrLi.length;i++){
                    arrLi[i].onclick=function(){
                        for(var i=0;i<arrLi.length;i++){
                            arrLi[i].className='';
                        }
                        this.className='hover';
                    }
                }
            }
        </script>
    </head>
    <body>
        <ul id="list">
            <li class="list">
                <h2>大学同学</h2>
                <ul>
                    <li>张三</li>
                    <li>张三</li>
                    <li>张三</li>
                    <li>张三</li>
                </ul>
            </li>
            <li class="list">
                <h2>高中同学</h2>
                <ul>
                    <li>李四</li>
                    <li>李四</li>
                    <li>李四</li>
                    <li>李四</li>
                </ul>
            </li>
            <li class="list">
                <h2>初中同学</h2>
                <ul>
                    <li>王五</li>
                    <li>王五</li>
                    <li>王五</li>
                    <li>王五</li>
                </ul>
            </li>
        </ul>
    </body>
</html>

欢迎分享本文,转载请保留出处:前端ABC » js模仿qq下拉列表

分享到:更多 ()

发表评论 0