用CSS完成Tab页切换结果

用CSS完成Tab切换结果

近来切一个页面的时刻触及到了一个tab切换的部份,由于不想用js想着能不能用纯CSS的挑选器来完成切换结果。搜了一下大抵有下面三种写法。

  1. 应用:hover挑选器

    • 瑕玷:只要鼠标在元素上面的时刻才有结果,没法完成选中和默许显现某一个的结果
  2. 应用a标签的锚点 + :target挑选器

    • 瑕玷:由于锚点会将选中的元素滚动到页面最上面,每次切换位置都要挪动,体验极差。
  3. 应用label和radio的绑定关联和radio选中时的:checked来完成结果

    • 瑕玷:HTML构造元素更庞杂

《用CSS完成Tab页切换结果》

经由试验发明第三种要领到达的结果最好。所以下面讲一下第三种完成的要领。

这类要领的写法不牢固,我查资料的时刻林林总总的写法都有一度让我一头雾水的。末了看完发明整体思绪都是一样的,不过就是下面的几个步骤。

  1. 绑定label和radio:这个不必说id和for属性绑定
  2. 隐蔽radio按钮:这个要领有许多充分发挥你们的想象力就能够了,我见过的要领有设置display:none;隐蔽的、设置相对定位,将left设置为很大的负值,挪动到页面外到达隐蔽结果、设置相对定位:使元素离开文档流,然后opacity: 0;设置为透明来到达隐蔽结果。
  3. 隐蔽过剩的tab页:和上面同理,还能够经由过程z-index设置层级关联来互相遮挡。
  4. 设置默许项:在默许按钮上增加checked="checked"属性
  5. 设置选中结果:应用+挑选器 和 ~挑选器来设置选中对应元素时下方的tab页的款式,来到达选中的结果

    /* 当radio为选中状况时设置它的test-label兄弟元素的属性 */
    input[type="radio"]:checked+.test-label {
        /* 为了润饰存在的边框背景属性 */
        border-color: #cbcccc;
        border-bottom-color: #fff;
        background: #fff;
        /* 为了润饰存在的层级使下边框遮挡下方div的上边框 */
        z-index: 10;
    }
    /* 当radio为选中状况时设置与它同级的tab-box元素的显现层级 */
    input[type="radio"]:checked~.tab-box {
        /* 选中时提拔层级,遮挡其他tab页到达选中切换的结果 */
        z-index: 5;
    }

如许就能够完成一个Tab页切换的结果了,不必一点儿js,固然一定也有兼容性的题目。实际操作中tab页照样运用js比较好。下面是小Demo的代码,款式比较多重要是为了完成种种选中结果,真正用来到达挑选切换目地的中心代码就几行

演示地点

代码:

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>CSS完成Tab切换结果</title>
    <style>
        ul {
            margin: 0;
            padding: 0;

        }
        .clearfloat {
            zoom: 1;
        }
        .clearfloat::after {
            display: block;
            clear: both;
            content: "";
            visibility: hidden;
            height: 0;
        }

        .tab-list {
            position: relative;
        }

        .tab-list .tab-itom {
            float: left;
            list-style: none;
            margin-right: 4px;
        }

        .tab-itom .test-label {
            position: relative;
            display: block;
            width: 85px;
            height: 27px;
            border: 1px solid transparent;
            border-top-left-radius: 5px;
            border-top-right-radius: 5px;
            line-height: 27px;
            text-align: center;
            background: #e7e8eb;
        }

        .tab-itom .tab-box {
            /* 设置相对定位轻易定位相对于tab-list栏的位置,同时为了能够运用z-index属性 */
            position: absolute;
            left: 0;
            top: 28px;
            width: 488px;
            height: 248px;
            border: 1px solid #cbcccc;
            border-radius: 5px;
            border-top-left-radius: 0px;
            background: #fff;
            /* 设置层级最低轻易选中状况遮挡 */
            z-index: 0;
        }
        /* 用相对定位使按钮离开文档流,透明度设置为0将其隐蔽 */
        input[type="radio"] {
            position: absolute;
            opacity: 0;
        }
        /* 应用挑选器完成  tab切换 */

        /* 当radio为选中状况时设置它的test-label兄弟元素的属性 */
        input[type="radio"]:checked + .test-label {
            /* 为了润饰存在的边框背景属性 */
            border-color: #cbcccc;
            border-bottom-color: #fff;
            background: #fff;
            /* 为了润饰存在的层级使下边框遮挡下方div的上边框 */
            z-index: 10;
        }
        /* 当radio为选中状况时设置与它同级的tab-box元素的显现层级 */
        input[type="radio"]:checked ~ .tab-box {
            /* 选中时提拔层级,遮挡其他tab页到达选中切换的结果 */
            z-index: 5;
        }
    </style>
</head>

<body class="clearfloat">
    <ul class="tab-list clearfloat">
        <li class="tab-itom">
            <input type="radio" id="testTabRadio1" class="test-radio" name="tab" checked="checked">
            <label class="test-label" for="testTabRadio1">选项卡一</label>
            <div class="tab-box">
                111111111111
            </div>
        </li>
        <li class="tab-itom">
            <input type="radio" id="testTabRadio2" class="test-radio" name="tab">
            <label class="test-label" for="testTabRadio2">选项卡二</label>
            <div class="tab-box">
                2222222222222
            </div>
        </li>
        <li class="tab-itom">
            <input type="radio" id="testTabRadio3" class="test-radio" name="tab">
            <label class="test-label" for="testTabRadio3">选项卡三</label>
            <div class="tab-box">
                33333333333333
            </div>
        </li>
    </ul>
</body>

</html>
    原文作者:徐小武
    原文地址: https://segmentfault.com/a/1190000013797371
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞