tab栏切换

css代码

<style>
        *{
            margin: 0;
            padding: 0;
        }
        li{
            list-style: none;
        }
        .tab{
            display: flex;
            width: 500px;
            height: 30px;
            align-items: center;
            background-color: #eee;
            border-bottom: 1px solid #f00;
        }
        .tab li{
            flex: 1;
            height: 30px;
            line-height: 30px;
            text-align: center;
            cursor: pointer;
        }
        /* .tab li:nth-child(1){
            background-color: #f00;
            color: #fff;
        } */
        .tab li.active{
            background-color: #f00;
            color: #fff;
        }
        .showList{
            display: flex;
            width: 498px;
            height: 200px;
            border: 1px solid #000;
            border-top: none;
        }
        .showList li{
            width: 100%;
            height: 100%;
            display: none;
        }
        /* .showList li:nth-child(1){
            display: block;
        } */
        .showList li.active{
            display: block;
        }
    </style>

 

html代码

<div class="container">
        <ui class="tab">
            <li class="active">商品介绍</li>
            <li>规格与包装</li>
            <li>售后保障</li>
            <li>商品评价</li>
            <li>预售说明</li>
        </ui>
        <ui class="showList">
            <li class="active">商品介绍模块</li>
            <li>规格与包装模块</li>
            <li>售后保障模块</li>
            <li>商品评价模块</li>
            <li>预售说明模块</li>
        </ui>
    </div>

script代码

 <script>
        // 获取元素
        var tabLis = document.querySelectorAll('.tab li');
        var showLis = document.querySelectorAll('.showList li');
        for(var i = 0;i<tabLis.length;i++){
            tabLis[i].setAttribute('index',i);
            tabLis[i].onclick = function (){
                for(var j = 0; j<tabLis.length;j++){
                    // tabLis[j].style.backgroundColor = 'transparent';
                    // tabLis[j].style.color = '#000';
                    // showLis[j].style.display = 'none';
                    tabLis[j].className = '';
                    showLis[j].className = '';
                }
                // this.style.backgroundColor = '#f00';
                // this.style.color = '#fff';
                this.className = 'active';
                var showIndex = this.getAttribute('index');
                // showLis[showIndex].style.display = 'block';
                showLis[showIndex].className = 'active';
            }
        }
    </script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值