JavaScript实现tab选项卡效果

css代码
<style>
        *{
            margin: 0;
            padding: 0;
        }
        .container{
            width: 500px;
            /*height: 700px;*/
            border: 1px solid #000;
            margin: 100px auto;

        }
        .container>ul{
            background-color: skyblue;
            list-style: none;
            display: flex;
            justify-content: space-between;
            width: 100%;
            height: 60px;
            line-height: 60px;
        }
        ul>li{
            width: 100px;
            font-size: 20px;
            border-right: 1px solid #000000;
            text-align: center;
            cursor: default;
        }
        ul>li:last-child{
            border-right: none;
        }

        .content{
            width: 100%;
            height: 300px;
        }

        .bottom>.content:nth-child(1){
            background-color: pink;
        }
        .bottom>.content:nth-child(2){
            background-color: purple;
            display: none;
        }
        .bottom>.content:nth-child(3){
            background-color: greenyellow;
            display: none;
        }
        .bottom>.content:nth-child(4){
            background-color: orange;
            display: none;
        }
        .bottom>.content:nth-child(5){
            background-color: darkcyan;
            display: none;
        }

        .current{
            background-color: brown;
        }
    </style>
html代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>tab选项卡</title>
    
</head>
<body>
    <div class="container">
        <ul>
            <li class="current">新闻</li>
            <li>科技</li>
            <li>数码</li>
            <li>游戏</li>
            <li>搞笑</li>
        </ul>

        <div class="bottom">
            <div class="content">新闻内容</div>
            <div class="content">科技内容</div>
            <div class="content">数码内容</div>
            <div class="content">游戏内容</div>
            <div class="content">搞笑内容</div>
        </div>

    </div>
    
</body>
</html>
JavaScript代码
<script>
        // 拿到所有的选项卡
        let items = document.querySelectorAll("li");

        // 拿到所有的内容
        let contents = document.querySelectorAll(".content");

        // 上一次选中的选项卡的索引
        let preIndex = 0;

        //  为所有选项卡添加单机响应事件
        for (let i = 0; i < items.length; i++) {
           items[i].onclick = function () {
              items[preIndex].className = "";
              contents[preIndex].style.display = "none";
              items[i].className = "current";
              contents[i].style.display = "block";
              preIndex = i;
           };
        }
    </script>
运行效果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值