【JS提升】选项卡的两种实现方式

一、选项卡的两种实现方式

<!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>
        .wrap {
            width: 500px;
            height: 500px;
            margin: 50px auto;
        }

        .tab {
            height: 50px;
        }

        .tab .item {
            float: left;
            width: 100px;
            height: 100%;
            text-align: center;
            line-height: 50px;
        }

        .tab .item.current {
            background-color: #000;
            color: #fff;
        }

        .page {
            position: relative;
            height: 450px;
            border: 1px solid #000;
        }

        .page .item {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            font-size: 100px;
            text-align: center;
            line-height: 450px;
        }

        .page .item.active {
            display: block;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <div class="tab J_tab">
            <div class="item current">选项1</div>
            <div class="item">选项2</div>
            <div class="item">选项3</div>
        </div>
        <div class="page J_page">
            <div class="item active">页面1</div>
            <div class="item">页面2</div>
            <div class="item">页面3</div>
        </div>
    </div>
</body>

</html>

(1)方法一

<script>
    ; (function (doc) {
        var oTab = doc.getElementsByClassName('J_tab')[0],
            oPage = doc.getElementsByClassName('J_page')[0],
            tabItems = oTab.getElementsByClassName('item'),
            pageItems = oPage.getElementsByClassName('item');
        var init = function () {
            bindEvent();
        }
        function bindEvent() {
            oTab.addEventListener('click', tabClick, false);
        }
        function tabClick() {
            var e = e || window.event,
                tar = e.target || e.srcElement,
                className = tar.className;
            if (className === 'item') {
                var curIdx = Array.prototype.indexOf.call(tabItems, tar);
                for (var i = 0; i < tabItems.length; i++) {
                    tabItems[i].className = 'item';
                    pageItems[i].className = 'item';
                }
                tar.className += ' current';
                pageItems[curIdx].className += ' active';
            }
        }
        init();
    })(document);
</script>

(2)方法二(不用for循环)

<script>
    ; (function (doc) {
        var oTab = doc.getElementsByClassName('J_tab')[0],
            oPage = doc.getElementsByClassName('J_page')[0],
            tabItems = oTab.getElementsByClassName('item'),
            pageItems = oPage.getElementsByClassName('item'),
            curIdx = 0;
        var init = function () {
            bindEvent();
        }
        function bindEvent() {
            oTab.addEventListener('click', tabClick, false);
        }
        function tabClick() {
            var e = e || window.event,
                tar = e.target || e.srcElement,
                className = tar.className;
            if (className === 'item') {
                tabItems[curIdx].className = 'item';
                pageItems[curIdx].className = 'item';
                curIdx = Array.prototype.indexOf.call(tabItems, tar);
                tar.className += ' current';
                pageItems[curIdx].className += ' active';
            }
        }
        init();
    })(document);
</script>

(3)优化版

<script>
    ; (function (doc) {
        var oTab = doc.getElementsByClassName('J_tab')[0],
            oPage = doc.getElementsByClassName('J_page')[0],
            tabItems = oTab.getElementsByClassName('item'),
            pageItems = oPage.getElementsByClassName('item'),
            curIdx = 0;
        var init = function () {
            bindEvent();
        }
        function bindEvent() {
            oTab.addEventListener('click', tabClick, false);
        }
        function tabClick() {
            var e = e || window.event,
                tar = e.target || e.srcElement,
                className = tar.className;
            if (className === 'item') {
                tabChange(tar);
            }
        }
        function tabChange(target) {
            tabItems[curIdx].className = 'item';
            pageItems[curIdx].className = 'item';
            curIdx = Array.prototype.indexOf.call(tabItems, target);
            tar.className += ' current';
            pageItems[curIdx].className += ' active';
        }
        init();
    })(document);
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值