tab切换动画

tab切换动画

效果图

代码

css部分

 * {
            margin: 0;
            padding: 0;
        }

        #titles {
            margin: 100px auto;
            list-style: none;
            width: 900px;
            height: 50px;
            overflow: hidden;
        }

        #titles>li {
            float: left;
            width: 300px;
            line-height: 50px;
            text-align: center;
            background-color: blue;
        }

        #titles>li:first-child {
            background-color: gold;
        }

        #contents {
            width: 900px;
            margin: 50px auto;
            list-style: none;
        }

        #contents>li {
            height: 400px;
            text-align: center;
            line-height: 400px;
        }

        #contents>li:first-child {
            background-color: aqua;
        }

        #contents>li:nth-child(2) {
            background-color: aquamarine;
            display: none;
        }

        #contents>li:last-child {
            background-color: blanchedalmond;
            display: none;
        }

html部分

<div>
        <ul id="titles">
            <li>标题一</li>
            <li>标题二</li>
            <li>标题三</li>
        </ul>
        <ul id="contents">
            <li>内容一</li>
            <li>内容二</li>
            <li>内容三</li>
        </ul>
    </div>

js部分

 var titles = document.getElementById('titles').getElementsByTagName('li');
    var contents = document.getElementById('contents').getElementsByTagName('li');
    // console.log(titles, contents);
    // 遍历每个title
    for (let i = 0; i < titles.length; i++) {
        //给每个title绑定点击事件
        titles[i].onclick = function () {
            for (let j = 0; j < titles.length; j++) {
                // console.log(this);
                if (j == i) {
                    contents[j].style.display = 'block'
                    titles[i].style.backgroundColor = 'gold'
                } else {
                    contents[j].style.display = 'none'
                    titles[j].style.backgroundColor = 'blue'
                }
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值