使用class写一个选项卡案例

此案例主要实现点击按钮时让对应的按钮高亮和内容区域显示不同的内容

案例效果展示:

 1.首先将html内容写好

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="tab">
        <!--按钮区域-->
        <ul>
            <li class="active">影片1</li>
            <li>影片2</li>
            <li>影片3</li>
            <li>影片4</li>
            <li>影片5</li>
        </ul>
        <!--内容区域-->
        <div class="con" data-hello="100">
            <div class="show">莫斯科行动</div>
            <div>孤注一掷</div>
            <div>坚如磐石</div>
            <div>变形金刚</div>
            <div>碟中谍7</div>
        </div>
    </div>

</body>

</html>

2.写css渲染样式

* {
            margin: 0;
            padding: 0;
        }

        .tab {
            width: 500px;
            min-height: 280px;
            background-color: orange;
            margin: 50px auto;
        }

        .tab ul {
            height: 45px;
            background-color: yellow;
            display: flex;
            justify-content: space-around;
            align-items: center;
        }

        .tab ul li {
            list-style: none;
            width: 20%;
            height: 100%;
            text-align: center;
            line-height: 45px;
            cursor: pointer;
        }

        .tab .con {
            height: 240px;
            background-color: #ccc;
            font-size: 14px;
            line-height: 100px;
        }

        .tab .con div {
            width: 100%;
            height: 100%;
            display: none;
        }

        .active {
            background-color: greenyellow;
        }

        .show {
            display: block !important;
        }

3.写入js实现按钮高亮是切换内容

class Tab {
            constructor(options) {
                // 解构赋值 
                const { btn, content } = options
                this.btns = document.querySelector(btn).children
                this.contents = document.querySelector(content).children
                console.log(this)

                this.handleClick()
            }
            // 点击按钮
            handleClick() {
                console.log('>>>', this);
                [...this.btns].forEach((it, index) => {
                    it.onclick = () => {
                        // 排他 高亮 
                        this.changeActive(index)
                    }
                })
            }
            changeActive(count) {
                //  this
                [...this.btns].forEach((it, index) => {
                    it.classList.remove('active')
                    this.contents[index].classList.remove('show')
                })

                // 让对应索引的高亮
                console.log(count)
                this.btns[count].classList.add('active')
                this.contents[count].classList.add('show')
            }
        }

        let t1 = new Tab({
            btn: '.tab>ul',
            content: '.con'
        })

这样就实现了案例所要实现的功能

完整代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .tab {
            width: 500px;
            min-height: 280px;
            background-color: orange;
            margin: 50px auto;
        }

        .tab ul {
            height: 45px;
            background-color: yellow;
            display: flex;
            justify-content: space-around;
            align-items: center;
        }

        .tab ul li {
            list-style: none;
            width: 20%;
            height: 100%;
            text-align: center;
            line-height: 45px;
            cursor: pointer;
        }

        .tab .con {
            height: 240px;
            background-color: #ccc;
            font-size: 14px;
            line-height: 100px;
        }

        .tab .con div {
            width: 100%;
            height: 100%;
            display: none;
        }

        .active {
            background-color: greenyellow;
        }

        .show {
            display: block !important;
        }
    </style>
</head>

<body>
    <div class="tab">
        <!--按钮区域-->
        <ul>
            <li class="active">影片1</li>
            <li>影片2</li>
            <li>影片3</li>
            <li>影片4</li>
            <li>影片5</li>
        </ul>
        <!--内容区域-->
        <div class="con" data-hello="100">
            <div class="show">莫斯科行动</div>
            <div>孤注一掷</div>
            <div>坚如磐石</div>
            <div>变形金刚</div>
            <div>碟中谍7</div>
        </div>
    </div>
    <script>

        class Tab {
            constructor(options) {
                // 解构赋值 
                const { btn, content } = options
                this.btns = document.querySelector(btn).children
                this.contents = document.querySelector(content).children
                console.log(this)

                this.handleClick()
            }
            // 点击按钮
            handleClick() {
                console.log('>>>', this);
                [...this.btns].forEach((it, index) => {
                    it.onclick = () => {
                        // 排他 高亮 
                        this.changeActive(index)
                    }
                })
            }
            changeActive(count) {
                //  this
                [...this.btns].forEach((it, index) => {
                    it.classList.remove('active')
                    this.contents[index].classList.remove('show')
                })

                // 让对应索引的高亮
                console.log(count)
                this.btns[count].classList.add('active')
                this.contents[count].classList.add('show')
            }
        }

        let t1 = new Tab({
            btn: '.tab>ul',
            content: '.con'
        })
    </script>
</body>

</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值