Tab切换

2019-06-25  本文已影响0人  Lamport
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title> Tab切换-JavaScript </title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            .box{
                width: 500px;
                height: 500px;
                margin: 50px auto 0;
                box-shadow: 0 0 4px #DDD;
                position: relative;
            }
            ul:after{
                content: "";
                height: 0;
                visibility: hidden;
                clear: both;
                display: block;
            }
            ul li{
                list-style: none;
                float: left;
                width: 100px;
                height: 50px;
                font-size: 25px;
                text-align: center;
                line-height: 50px;
                background: greenyellow;
            }
            ul li.active{
                background: blue;
            }
            .box div{
                width: 500px;
                height: 450px;
                background: red;
                font-size: 200px;
                text-align: center;
                line-height: 450px;
                position: absolute;
                bottom: 0;
                left: 0;
            }
            .box div.active{
                z-index: 2;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <ul id="list">
                <li class="active">01</li>
                <li>02</li>
                <li>03</li>
                <li>04</li>
                <li>05</li>
            </ul>
            <div class="active">01</div>
            <div>02</div>
            <div>03</div>
            <div>04</div>
            <div>05</div>
        </div>
        <script type="text/javascript">
            var oBox = document.getElementsByTagName("div")[0];
            var oList = document.getElementById("list");
            var oLi = oList.getElementsByTagName("li");
            var oDiv = oBox.getElementsByTagName("div");
            
            for( var i=0;i<oLi.length;i++ ){
                oLi[i].index = i;
                oLi[i].onclick = function(){
                    for( var j=0;j<oDiv.length;j++ ){
                        oLi[j].className = '';
                        oDiv[j].className = '';
                    };
                    this.className = 'active';
                    oDiv[this.index].className = 'active';
                };
            };
        </script>
    </body>
</html>

上一篇 下一篇

猜你喜欢

热点阅读