WEB 4 JS做选项卡效果

成果展示

image.png

代码展示

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>选项卡效果</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        html,body{
            height: 100%;
            width: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        html{font-size: 10px;}
        .tab{
            width: 40rem;
            height: 30rem;
            border: solid 1px #000;
            
        }
        .tab-title{
            width: 100%;
            height: 5rem;
            background-color: antiquewhite;
            border-bottom: solid 2px orange;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .tt{
            flex:1;
            height: 5rem;
            background-color: aliceblue;
            font-size: 1.8rem;
            text-align: center;
            line-height: 5rem;
        }
        .tt.active,
        .tt:hover{
            background-color: orange;
            color: white;
        }
        .tab-content{
            width: 100%;
            height:248px;
            background-color: aquamarine;
            position: relative;
        }
        .tc{
            width: 100%;
            height: 100%;
            font-size: 1.8rem;
            text-align: center;
            color: white;
            position: absolute;
        }
        .tc:nth-last-of-type(1){
            background-color: rgb(255, 0, 21);
        }
        .tc:nth-last-of-type(2){
            background-color: green;
        }
        .tc:nth-last-of-type(3){
            background-color:rgb(0, 42, 255);
        }

    </style>
</head>
<body>
    <div class="tab">
        <div class="tab-title">
            <div class="tt active">标题1</div>
            <div class="tt">标题2</div>
            <div class="tt">标题3</div>
        </div>
        <div class="tab-content">
            <div class="tc">内容1</div>
            <div class="tc">内容2</div>
            <div class="tc">内容3</div>
        </div>
    </div>
    <script>
        let tts=document.getElementsByClassName("tt")
        console.log(tts)
        for(let i=0;i<tts.length;i++){
            tts[i].onmouseenter=function(){
                for(let j=0;j<tts.length;j++){
                    tts[j].classList="tt"
                }
                tts[i].classList="tt active"
                let tcs=document.getElementsByClassName("tc")
                for(let x=0;x<tcs.length;x++){
                    tcs[i].classList="tc"
                }
                tcs[i].classList="tc active"
            }
        }
    </script>
</body>
</html>

注意

getElementsByClassName等中的大小写不要打错了,否则代码会出现错误
细节自己调试

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1,javascript 基础知识 Array对象 Array对象属性 Arrray对象方法 Date对象 Dat...
    Yuann阅读 992评论 0 1
  • 一、js的基本类型有哪些?引用类型有哪些?null和undefined的区别?1、基本类型Number,Strin...
    林立镇阅读 825评论 0 18
  • 上来先说说实现思路 首先 我们要做什么 ?eg: 通过点击 让对应的页面展示出来这里我们是否需要获取到要点击的元...
    手握一颗心阅读 298评论 0 0
  • 一:在制作一个Web应用或Web站点的过程中,你是如何考虑他的UI、安全性、高性能、SEO、可维护性以及技术因素的...
    Arno_z阅读 1,277评论 0 1
  • css代码 html代码 js代码
    Im明猪阅读 193评论 0 1