纯css3实现hover选项卡效果

原理:利用css hover可以控制同级或同级子级元素

<DOCTYPE html>
    <html>
    <head>
        <title>Tab选项卡</title>
        <meta charset="utf-8">
        <style type="text/css">
            body{
                font-size: 0;
            }
            .box{
                text-align: center;
            }
            .box .tab{
                display: inline-block;
                width: 120px;
                height: 44px;
                padding: 7px;
                border: 1px solid #ccc;
                border-bottom: 0px;
                box-sizing:border-box;
                background: #fff;
                font-size: 16px;
                line-height: 26px;
                color: #555;
                transition: all 0.5s linear;
            }
            .box .tab:hover{
                background: #eee;
                transition: all 0.5s linear;
            }
            .con{
                width: 800px;
                height: 400px;
                margin:0 auto;
            }
            .con .list{
                width: 800px;
                height: 400px;
                border: 1px solid #ccc;
                padding: 10px;
                position: absolute;/*堆一起*/
                z-index: 1;
                box-sizing:border-box;
            }
            .list img{
                height: 300px;
                width: auto;
                margin: 40px auto;
            }
            .box>.tab:nth-child(1):hover~.con>.list:nth-child(1),
            .box>.tab:nth-child(2):hover~.con>.list:nth-child(2),
            .box>.tab:nth-child(3):hover~.con>.list:nth-child(3),
            .box>.tab:nth-child(4):hover~.con>.list:nth-child(4),
            .list:hover{
                z-index: 3;
            }
        </style>
    </head>
    <body>
    <div class="box">
        <a class="tab">哇咔咔</a>
        <a class="tab">太棒了</a>
        <a class="tab">纳尼</a>
        <a class="tab">不要听</a>
        <div class="con">
            <div class="list"><img src="../img/1.png"></div>
            <div class="list"><img src="../img/2.png"></div>
            <div class="list"><img src="../img/3.png"></div>
            <div class="list"><img src="../img/4.png"></div>
        </div>
    </div>
    </body>
    </html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在前端实现选项卡切换内容,可以使用HTML、CSS和JavaScript来实现。以下是一个简单的示例: HTML代码: ```html <div class="tab"> <button class="tablinks" onclick="openTab(event, 'tab1')">选项卡1</button> <button class="tablinks" onclick="openTab(event, 'tab2')">选项卡2</button> <button class="tablinks" onclick="openTab(event, 'tab3')">选项卡3</button> </div> <div id="tab1" class="tabcontent"> <h3>选项卡1</h3> <p>这是选项卡1的内容。</p> </div> <div id="tab2" class="tabcontent"> <h3>选项卡2</h3> <p>这是选项卡2的内容。</p> </div> <div id="tab3" class="tabcontent"> <h3>选项卡3</h3> <p>这是选项卡3的内容。</p> </div> ``` CSS代码: ```css .tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; } .tab button:hover { background-color: #ddd; } .tab button.active { background-color: #ccc; } .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; } ``` JavaScript代码: ```javascript function openTab(evt, tabName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(tabName).style.display = "block"; evt.currentTarget.className += " active"; } ``` 这个示例中,我们使用了HTML的`button`元素来作为选项卡的标签,使用`div`元素来作为选项卡的内容。CSS用于定义选项卡的样式,JavaScript用于实现选项卡的切换功能。 在JavaScript代码中,`openTab`函数接受两个参数,第一个参数是事件对象,第二个参数是要显示的选项卡的ID。函数首先隐藏所有选项卡的内容(通过将它们的`display`属性设置为`none`),然后将指定选项卡的内容显示出来(通过将其`display`属性设置为`block`),最后将当前选项卡的按钮标签的`active`类设置为激活状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值