干货知识!!!——普通css代码与js代码实现Tab菜单栏的切换效果的代码比较(文本内容随着切换内容的改变而显示与隐藏)


css版本1⃣️:



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>按照顺序传递参数</title>
    <style>
        h2 {
            border-top: solid cornflowerblue 1px;
            border-left: solid cornflowerblue 1px;
            width: 50px;
            height: 25px;
            margin: 0;
            float: left;
            text-align: center;
        }
        .tab-content {
            border: solid cornflowerblue 1px;
            width: 152px;
            height: 100px;
        }
        .tab-content div{
            display: none;
        }
        .selected {
            background-color: cornflowerblue;
        }
        .tab-content .show{
            display: block;
        }
    </style>
</head>

<body>
    <div class="tab-head">
        <h2 class="selected c1 c2 c3">1</h2>
        <h2 >2</h2>
        <h2 >3</h2>
    </div>
    <div class="tab-content">
        <div class="show">content1</div>
        <div>content2</div>
        <div>content3</div>
    </div>
</body>
<script>
    var tabs=document.getElementsByClassName("tab-head")[0].getElementsByTagName("h2"),
        contents=document.getElementsByClassName("tab-content")[0].getElementsByTagName("div");
      
    for(var i=0;i<tabs.length;i++){
      tabs[i].onmouseover=function(){
          for(var i=0;i<contents.length;i++){
            if (tabs[i] == this) {    // 显示当前鼠标滑过的li元素
              tabs[i].classList.add('selected');
              contents[i].classList.add('show');
            } else {                  // 隐藏其他li元素
              tabs[i].classList.remove('selected');
              contents[i].classList.remove('show');
            
          }
      }
    }
}
</script>
</html>

在这里插入图片描述


css版本2⃣️:


<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>标签栏切换效果</title>
    <style>
        body,
        ul {
            margin: 0;
            padding: 0;
        }

        ul {
            list-style: none;
        }

        .tab-box {
            width: 383px;
            margin: 10px;
            border: 1px solid #ccc;
            border-top: 2px solid #222222;
        }

        .tab-head {
            height: 31px;
        }

        .tab-head-div {
            width: 95px;
            height: 30px;
            float: left;
            border-bottom: 1px solid #ccc;
            border-right: 1px solid #ccc;
            background: #0cc6f5;
            line-height: 30px;
            text-align: center;
            cursor: pointer;
            color: #fff;
        }

        .tab-head .current {
            background: #fff;
            border-bottom: 1px solid #fff;
            color: #000;
        }

        .tab-head-r {
            border-right: 0;
        }

        .tab-body-div {
            display: none;
            margin: 20px 10px;
        }

        .tab-body .current {
            display: block;
        }
    </style>
</head>

<body>
    <div class="tab-box">
        <div class="tab-head">
            <div class="tab-head-div current">标签一</div>
            <div class="tab-head-div">标签二</div>
            <div class="tab-head-div">标签三</div>
            <div class="tab-head-div tab-head-r">标签四</div>
        </div>
        <!--jkdjfk?-->
        <div class="tab-body">
            <div class="tab-body-div current"> a </div>
            <div class="tab-body-div"> b </div>
            <div class="tab-body-div"> c </div>
            <div class="tab-body-div"> d </div>
        </div>
    </div>
    <script>
        // 获取标签栏的所有标签元素对象
        var tabs = document.getElementsByClassName('tab-head-div');
        // 获取标签栏的所有内容对象
        var divs = document.getElementsByClassName('tab-body-div');
         // 第一个for循环是遍历第一栏,鼠标悬停事件
        for (var i = 0; i < tabs.length; ++i) {        // 遍历标签部分的元素对象,从下标1到3,就是标签二到标签四
            tabs[i].onmouseover = function () {           // 为标签元素对象添加鼠标滑过事件
                for (var i = 0; i < divs.length; ++i) {    // 遍历标签栏的内容元素对象
                    if (tabs[i] == this) {    // 显示当前鼠标滑过的标签
                        divs[i].classList.add('current');//为元素添加类
                        tabs[i].classList.add('current');
                    } else {  // 当鼠标移动到标签一时,隐藏其他标签
                        divs[i].classList.remove('current');//移除类
                        tabs[i].classList.remove('current');
                    }
                }
            };
        }
    </script>
</body>

<html>

在这里插入图片描述


css版本3⃣️:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .tap_head{
            width: 390px;
            height: 80px;
            border: 1px solid black;
            background-color: blanchedalmond;
            margin: 10px;
        }
        .tap_head .current{
            
            color: #000;
        }
        .tap_head_div{
            width: 95px;
            height: 30px;
            float: left;
            border-bottom: 1px solid #ccc;
            border-right: 1px solid #ccc;
            background: #0cc6f5;
            line-height: 30px;
            text-align: center;
            color: #fff;
        }
        .tap_body_div{
            display: none;
        }
        .tap-body .current{
            display: block;
        }
    </style>
</head>
<body>
    <div class="tap_head">
        <div class="tap">
            <div class="tap_head_div current">标签一</div>
            <div class="tap_head_div">标签二</div>
            <div class="tap_head_div">标签三</div>
            <div class="tap_head_div">标签四</div>
        </div>
        <div class="tap-body">
            <div class="tap_body_div current">one</div>
            <div class="tap_body_div">two</div>
            <div class="tap_body_div">three</div>
            <div class="tap_body_div">four</div>
        </div>
    </div>
</body>
<script>
    var tap_div=document.getElementsByClassName("tap_head_div");
    var tap_body=document.getElementsByClassName("tap_body_div");
    for(var i=0;i<tap_div.length;i++){
        tap_div[i].onmouseover=function(){
        for(var i=0;i<tap_body.length;i++){
            if(tap_div[i]==this){
                tap_div[i].classList.add("current");
                tap_body[i].classList.add("current");
            }else{
                tap_div[i].classList.remove("current");
                tap_body[i].classList.remove("current");
            }
        }
    }  
 }
</script>
<html>

在这里插入图片描述


css版本4⃣️——升级版:



<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      li {
        list-style-type: none;
      }

      .tab {
        width: 978px;
        margin: 100px auto;
      }

      .tab_list {
        height: 39px;
        border: 1px solid #ccc;
        background-color: #f1f1f1;
      }

      .tab_list li {
        float: left;
        height: 39px;
        line-height: 39px;
        padding: 0 20px;
        text-align: center;
        cursor: pointer;
      }

      .tab_list .current {
        background-color: #c81623;
        color: #fff;
      }

      .item_info {
        padding: 20px 0 0 20px;
      }

      .item {
        display: none;
      }
    </style>
  </head>
  <body>
    <div class="tab">
      <div class="tab_list">
        <ul>
          <li class="current">商品介绍</li>
          <li>规格与包装</li>
          <li>售后保障</li>
          <li>商品评价(50000)</li>
          <li>手机社区</li>
        </ul>
      </div>
      <div class="tab_con">
        <div class="item" style="display: block;">商品介绍模块内容</div>
        <div class="item">规格与包装模块内容</div>
        <div class="item">售后保障模块内容</div>
        <div class="item">商品评价(50000)模块内容</div>
        <div class="item">手机社区模块内容</div>
      </div>
    </div>
    <script>
      // 获取标签部分的所有元素对象
      var tab_list = document.querySelector('.tab_list');
      var lis = tab_list.querySelectorAll('li');
      // 获取内容部分的所有内容对象
      var items = document.querySelectorAll('.item');
      for (var i = 0; i < lis.length; i++) {	// for循环绑定点击事件
        lis[i].setAttribute('index', i);		// 开始给5个小li设置索引号
        lis[i].onclick = function () {
          for (var i = 0; i < lis.length; i++) {
            lis[i].className = '';
          }
          this.className = 'current';
          // 下面的显示内容模块
          var index = this.getAttribute('index');
          for (var i = 0; i < items.length; i++) {
            items[i].style.display = 'none';
          }
          items[index].style.display = 'block';
        };
      }
    </script>
  </body>
</html>

在这里插入图片描述


js实现版本——极为简单实用:



<!DOCTYPE html>
<html lang="en">
    <script src="../js/jquery-1.12.4.js"></script>


<head>
    <meta charset="UTF-8">
    <title>按照顺序传递参数</title>
    <style>
        h2 {
            border-top: solid cornflowerblue 1px;
            border-left: solid cornflowerblue 1px;
            width: 50px;
            height: 25px;
            margin: 0;
            float: left;
            text-align: center;
        }
        .tab-content {
            border: solid cornflowerblue 1px;
            width: 152px;
            height: 100px;
        }
        .tab-content div{
            display: none;
        }
        .selected {
            background-color: cornflowerblue;
        }
        /* .tab-content .shows{
            display: block;
        } */
    </style>
</head>

<body>
    <div class="tab-head">
        <h2 class="selected">1</h2>
        <h2 >2</h2>
        <h2 >3</h2>
    </div>
    <div class="tab-content">
        <div style="display: block;">content1</div>
        <div>content2</div>
        <div>content3</div>
    </div>
</body>
<script>


    $(".tab-head h2").mouseover(function(){
        $(this).addClass("selected").siblings().removeClass("selected");
        var index = $(this).index();
        $(".tab-content div").eq(index).show().siblings().hide();
});


</script>
</html>

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱睡觉的小馨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值