美文网首页饥人谷技术博客
原生JS实现tab切换效果

原生JS实现tab切换效果

作者: 辉夜乀 | 来源:发表于2017-04-22 13:16 被阅读54次

    效果页面
    jsbin代码

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            * {
                box-sizing: border-box;
            }
    
            .clearfix:after {
                content: "";
                display: block;
                clear: both;
            }
    
            ul,
            li {
                list-style: none;
                margin: 0;
                padding: 0;
            }
    
            #tab {
                width: 600px;
                border: 1px solid #ccc;
                border-radius: 5px;
                margin: 0 auto;
            }
    
            .tab-header>ul>li {
                float: left;
                /*width: 33.33%;*/
                padding: 10px 20px;
                border: 1px solid #ccc;
                border-radius: 5px;
                cursor: pointer;
            }
    
            .tab-header>ul .oncls {
                background: #ccc;
            }
    
            .tab-content {
                height: 300px;
                background: #eee;
            }
    
            .tab-content>ul>li {
                display: none;
            }
    
            .tab-content>ul>li img {
                width: 100%;
                height: 100%;
            }
    
            .tab-content>ul .see {
                display: block;
            }
        </style>
    </head>
    
    <body>
        <section id="tab">
            <div class="tab-header">
                <ul class="clearfix">
                    <li class="oncls">道路</li>
                    <li>海滩</li>
                    <li>稻谷</li>
                </ul>
            </div>
            <div class="tab-content">
                <ul>
                    <li class="see">![](https://img.haomeiwen.com/i5051517/d468f24614a5a192.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</li>
                    <li>![](https://img.haomeiwen.com/i5051517/ad59e7b6626d25e2.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</li>
                    <li>![](https://img.haomeiwen.com/i5051517/f51f663ae90c0d77.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</li>
                </ul>
            </div>
        </section>
    
        <script>
            function $(str) {
                return document.querySelector(str);
            }
            var tabHeader = $(".tab-header>ul");
            var tabCt = $(".tab-content>ul")
            tabHeader.addEventListener("click", function(e) {
                if (e.target.tagName.toLowerCase() === "li") {
                    for (var i = 0; i < tabHeader.children.length; i++) {
                        tabHeader.children[i].classList.remove("oncls");
                    }
                    e.target.classList.add("oncls");
                    for (var i = 0; i < tabCt.children.length; i++) {
                        tabCt.children[i].classList.remove("see");
                    }
                    var index = [].indexOf.call(tabHeader.children, e.target);
                    tabCt.children[index].classList.add("see");
                }
            })
        </script>
    </body>
    
    </html>
    
    

    相关文章

      网友评论

        本文标题:原生JS实现tab切换效果

        本文链接:https://www.haomeiwen.com/subject/mxzjzttx.html