js实现Tab选项卡切换效果

若要前行,就得离开你现在停留的地方。要有对自己纯粹而坚韧的标准,才可以跟着直觉和信念走去。自卑心最伤害自己,是最大的障碍。有时候阳光很好,有时候阳光很暗,这就是生活。

本文实例为大家分享了js实现Tab选项卡切换效果展示的具体代码,供大家参考,具体内容如下

html部分

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" rel="nofollow noopener noreferrer" href="tab.css">
  <script src="tab.js"></script>
  <title>Document</title>
</head>
<body>
  <div id="tab">
    <div id="tab-nav" class="tab-nav">
      <ul>
        <li class="active"><a rel="nofollow noopener noreferrer" href="#">公告</a></li>
        <li><a rel="nofollow noopener noreferrer" href="#">规则</a></li>
        <li><a rel="nofollow noopener noreferrer" href="#">论坛</a></li>
        <li><a rel="nofollow noopener noreferrer" href="#">安全</a></li>
        <li><a rel="nofollow noopener noreferrer" href="#">公益</a></li>
      </ul>
    </div>
    <div id="tab-contain">
      <div class="mod">
        <ul>
          <li><a rel="nofollow noopener noreferrer" href="#">走进无声课堂</a></li>
          <li><a rel="nofollow noopener noreferrer" href="#">淘宝之行大众评审</a></li>
          <li><a rel="nofollow noopener noreferrer" href="#">爱心品牌联合征集</a></li>
          <li><a rel="nofollow noopener noreferrer" href="#">公益机构淘宝攻略</a></li>
        </ul>
      </div>
      <div class="mod" style="display:none">
        <ul>
          <li><a rel="nofollow noopener noreferrer" href="#">[聚焦]金牌卖家再启航</a></li>
          <li><a rel="nofollow noopener noreferrer" href="#">[功能]橱柜规则升级啦</a></li>
          <li><a rel="nofollow noopener noreferrer" href="#">[话题]又爱又恨优惠券</a></li>
          <li><a rel="nofollow noopener noreferrer" href="#">[工具] 购后送店铺红包</a></li>
        </ul>
      </div>
      <div class="mod" style="display:none">
        <ul>
          <li><a rel="nofollow noopener noreferrer" href="#">张勇:要玩快乐足球</a></li>
          <li><a rel="nofollow noopener noreferrer" href="">阿里2000万驰援灾区</a></li>
          <li><a rel="nofollow noopener noreferrer" href="">旅游宝让你边玩</a></li>
          <li><a rel="nofollow noopener noreferrer" href="">多行跟进阿里信用贷款</a></li>
        </ul>
      </div>
      <div class="mod" style="display:none">
        <ul>
          <li><a rel="nofollow noopener noreferrer" href="">[通知] “泛滥换新”</a></li>
          <li><a rel="nofollow noopener noreferrer" href="">[通知]“比特币严管”</a></li>
          <li><a rel="nofollow noopener noreferrer" href="">[通知]“禁发商品”</a></li>
          <li><a rel="nofollow noopener noreferrer" href="">[通知]“商品属性”</a></li>
        </ul>
      </div>
      <div class="mod" style="display:none">
        <ul>
          <li><a rel="nofollow noopener noreferrer" href="#">走进无声课堂</a></li>
          <li><a rel="nofollow noopener noreferrer" href="#">淘宝之行大众评审</a></li>
          <li><a rel="nofollow noopener noreferrer" href="#">爱心品牌联合征集</a></li>
          <li><a rel="nofollow noopener noreferrer" href="#">公益机构淘宝攻略</a></li>
        </ul>
      </div>
    </div>
  </div>
</body>
</html>

css部分

*{
 padding:0px;
 margin:0px;
 list-style: none;
 font-size: 14px;
}
#tab{
 width:298px;
 height:120px;
 margin:10px;
 border:1px solid #eee;
 overflow: hidden;
}
.tab-nav{
 width:400px;
 position:relative;
 height:27px;

}
.tab-nav ul{
 position:absolute;
 width:301px;
 left:-1px;
 background-color: #f7f7f7;
}
.tab-nav li{
 float:left;
 width:58px;
 padding: 0 1px;
 height:36px;
 background-color: #f7f7f7;
 border-bottom: 1px solid #eee;
 text-align: center;
}
.tab-nav li.active{
 background-color: #fff;
 border-bottom-color:#fff;
 border-left: 1px solid #eee;
 border-right: 1px solid #eee;
 padding:0px;
 font-weight: bolder;
}
li a:link,li a:visited{
 text-decoration: none;
 color:#000;
}
#tab-contain .mod{
 margin:25px 6px 10px 6px;
}
#tab-contain .mod ul li{
 float: left;
 width:143px;
 height:25px;
 overflow: hidden;
}

Js部分

function $(id){
 return typeof id==='string'?document.getElementById(id):id;
}
window.onload=function(){
 var navs=$('tab-nav').getElementsByTagName('li');
 var divs=$('tab-contain').getElementsByTagName('div');
 // alert(divs.length);
 if(navs.length!=divs.length){
  return;
 }
 for(var i=0;i<navs.length;i++){
  navs[i].title=i;
  navs[i].onmouseover=function(){
   for(var j=0;j<navs.length;j++){
    navs[j].className="";
    divs[j].style.display="none";

   }
   this.className="active";
   divs[this.title].style.display="block";
  }
 }

}

学习课程链接。

本文js实现Tab选项卡切换效果到此结束。冲动是魔鬼,因为它只会害人。遇事要冷静对待。小编再次感谢大家对我们的支持!

标签: 选项卡 js