用JS实现选项卡效果

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    html, body {
        width: 100%;
        height: 100%;
    }
    html{
        font-size: 10px;
    }
    .tab{
        width:40rem;
        height:35rem;
        background-color: rgb(219, 242, 242);
    }
    .tab-title{
        width:40rem;
        height:5rem;
        background-color: #e4c2c2;
        display: flex;
        justify-content:space-between;
        flex-direction: row;
        align-items: center;
        border:solid 2px rgb(231, 214, 190);
    }
    .tt{
        width:13.33rem;
        height:100%;
        background-color: #eff0f2;
        border-right:solid 2px rgb(226, 210, 191);
        text-align: center;
        font-size:2rem;
    }
    .tt:hover{
        cursor:pointer;
        background-color: #4089b7;
        color:white;
    }
    .tab-content{
        width:100%;
        height:calc(100% - 5rem);
        background-color: aliceblue;
        border:solid 2px rgb(190, 190, 237);
        border-top:none;
        position: relative;
    }
    .tc{
        width:100%;
        height:100%;
        background-color: aquamarine;
        text-align:center;
        font-size:2rem;
        position: absolute;
        display:none;
    }
    .tc:nth-of-type(1){
        background-color: #e47272;
        display: block;
    }
    .tc:nth-of-type(2){
        background-color: #a2d740;
    }
    .tc:nth-of-type(3){
        background-color: #ad5c97;
    }

</style>
<body>
    <div class="tab">
        <div class="tab-title">
            <div class="tt">标题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")
        for(let i=0; i<tts.length;i++){
            tts[i].onmouseenter=function(){
                let tsc=document.getElement
            }
        }
    </script>
</body>
</html>

效果呈现:


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

推荐阅读更多精彩内容