实现tab栏的三种方法

7 篇文章 0 订阅

用css锚点实现tab栏切换

代码部分

<!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>
      * {
        padding: 0;
        margin: 0;
        list-style: none;
        text-decoration: none;
      }
      .box {
        width: 500px;
        height: 500px;
        background-color: #bfa;
        margin: 0 auto;
      }
      .title {
        width: 100%;
        height: 50px;
        background-color: blue;
      }
      .title > a {
        display: inline-block;
        width: 100px;
        line-height: 50px;
        background-color: #ccc;
        color: #fff;
        font-weight: bold;
        text-align: center;
      }
      .title > a:hover {
        background-color: #333;
      }
      .content {
        width: 100%;
        height: 450px;
        background-color: pink;
        overflow: hidden;
      }
      .content>div{
          width: 100%;
          height: 100%;
          font-size: 50px;
          line-height: 450px;
          text-align: center;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="title">
        <a href="#box1">页面1</a>
        <a href="#box2">页面2</a>
        <a href="#box3">页面3</a>
      </div>
      <div class="content">
        <div id="box1">box1</div>
        <div id="box2">box2</div>
        <div id="box3">box3</div>
      </div>
    </div>
  </body>
</html>

原理:

锚点功能就是通过使用超链接来实现,在需要跳转的地方加上id属性,设置一个id值,然后在href属性里填上属性值,点击就可以跳转到相应的位置。使用以上思想来实现tab栏的点击切换。

结构伪类target实现tab栏切换

代码部分

<!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>
    <!-- :target选择器称为目标选择器,
      用来匹配文档(页面)的url的某个标志符的目标元素。 -->
    <style>
      /*这里的:target就是指id="brand"的div对象*/
      #brand:target {
        background: orange;
        color: #fff;
      }
      #jake:target {
        background: blue;
        color: #fff;
      }
      #aron:target {
        background: red;
        color: #fff;
      }
      /* h2:active+div{
        background-color: red;
      } */
    </style>
  </head>
  <body>
    <h2><a href="#brand">Brand</a></h2>
    <div class="menuSection" id="brand">content for Brand</div>

    <h2><a href="#jake">Brand</a></h2>
    <div class="menuSection" id="jake">content for jake</div>

    <h2><a href="#aron">Brand</a></h2>
    <div class="menuSection" id="aron">content for aron</div>
  </body>
</html>

原理:

先根据target的特性锚链接到对应的div,再根据z-index的属性,改变div的层级关系,从而实现tab的切换效果!

lable实现tab栏切换

代码部分

<!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;
        list-style: none;
      }
      ul {
        position: relative;
        width: 500px;
        margin: 100px auto;
      }
      ul li input {
        display: none;
      }
      ul li label {
        float: left; 
         width: 150px;
        text-align: center;
        line-height: 30px;
        border: 1px solid red;
        box-sizing: border-box;
        cursor: pointer;
      }
      ul li input:checked + label {
        color: #fff;
        background-color: #000;
      }
      ul li .content {
        visibility: hidden;
        position: absolute;
        left: 0;
        top: 31px;
        width: 100%;
        height: 300px;
        border: 1px solid #000;
        font-size: 24px;
        text-align: center;
        line-height: 300px;
        color: #fff;
      }
      ul li input:checked ~ .content {
        visibility: visible;
      }
      ul li:nth-child(1) .content {
        background-color: #0f0;
      }
      ul li:nth-child(2) .content {
        background-color: #00f;
      }
      ul li:nth-child(3) .content {
        background-color: #f0f;
      }

      /* 
      ① input需要隐藏,因为我们并不需要显示它,但它却是实现Tab切换的核心力量
      ② “input:checked+label” 表示被选中的单选框后的 label 元素需要做标记
      ③ .content 元素需要先全部隐藏
      ③ “input:checked~.content” 表示被选中的单选框后的 .content 元素需要显示
 */
    </style>
  </head>
  <body>
    <!--① label需要绑定input,方法就是label的for属性值与input的id一致,
      这样当点击label元素时input单选框就会被选中
        ② input、label和div三者是有顺序的,不能随意调换顺序-->
    <ul>
      <li>
        <input id="tab1" type="radio" name="tab" checked />
        <label for="tab1">选项一</label>
        <div class="content">选项一内容</div>
      </li>
      <li>
        <input id="tab2" type="radio" name="tab" />
        <label for="tab2">选项二</label>
        <div class="content">选项二内容</div>
      </li>
      <li>
        <input id="tab3" type="radio" name="tab" />
        <label for="tab3">选项三</label>
        <div class="content">选项三内容</div>
      </li>
    </ul>
  </body>
</html>

原理:

当用户点击label元素时,该label所绑定的input单选框就会被选中,同时通过使用CSS选择器让被选中的input元素之后的label和.content元素都加上相应的样式。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值