使用js实现tab页签切换效果

30 篇文章 0 订阅

效果图如下

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  2. "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  6.         <title>New Web Project</title>  
  7.         <style type="text/css">  
  8.             #content {  
  9.                 width: 400px;  
  10.                 height: 200px;  
  11.             }  
  12.               
  13.             #tab_bar {  
  14.                 width: 400px;  
  15.                 height: 20px;  
  16.                 float: left;  
  17.             }  
  18.             #tab_bar ul {  
  19.                 padding: 0px;  
  20.                 margin: 0px;  
  21.                 height: 20px;  
  22.                 text-align: center;  
  23.             }  
  24.               
  25.             #tab_bar li {  
  26.                 list-style-type: none;  
  27.                 float: left;  
  28.                 width: 133.3px;  
  29.                 height: 20px;  
  30.                 background-color: gray;  
  31.             }  
  32.               
  33.             .tab_css {  
  34.                 width: 400px;  
  35.                 height: 200px;  
  36.                 background-color: orange;  
  37.                 display: none;  
  38.                 float: left;  
  39.             }  
  40.               
  41.         </style>  
  42.         <script type="text/javascript">  
  43.             var myclick = function(v) {  
  44.                 var llis = document.getElementsByTagName("li");  
  45.                 for(var i = 0; i < llis.length; i++) {  
  46.                     var lli = llis[i];  
  47.                     if(lli == document.getElementById("tab" + v)) {  
  48.                         lli.style.backgroundColor = "orange";  
  49.                     } else {  
  50.                         lli.style.backgroundColor = "gray";  
  51.                     }  
  52.                 }  
  53.   
  54.                 var divs = document.getElementsByClassName("tab_css");  
  55.                 for(var i = 0; i < divs.length; i++) {  
  56.   
  57.                     var divv = divs[i];  
  58.   
  59.                     if(divv == document.getElementById("tab" + v + "_content")) {  
  60.                         divv.style.display = "block";  
  61.                     } else {  
  62.                         divv.style.display = "none";  
  63.                     }  
  64.                 }  
  65.   
  66.             }  
  67.         </script>  
  68.     </head>  
  69.     <body>  
  70.         <div id="content">  
  71.             <div id="tab_bar">  
  72.                 <ul>  
  73.                     <li id="tab1" onclick="myclick(1)" style="background-color: orange">  
  74.                         tab1  
  75.                     </li>  
  76.                     <li id="tab2" onclick="myclick(2)">  
  77.                         tab2  
  78.                     </li>  
  79.                     <li id="tab3" onclick="myclick(3)">  
  80.                         tab3  
  81.                     </li>  
  82.                 </ul>  
  83.             </div>  
  84.             <div class="tab_css" id="tab1_content" style="display: block">  
  85.                 <div>页面一</div>  
  86.             </div>  
  87.             <div class="tab_css" id="tab2_content">  
  88.                 <div>页面二</div>  
  89.             </div>  
  90.             <div class="tab_css" id="tab3_content">  
  91.                 <div>页面三</div>  
  92.             </div>  
  93.         </div>  
  94.     </body>  
  95. </html>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的jQuery实现tab标签滑动切换效果的示例代码: HTML结构: ```html <div class="tab-wrap"> <ul class="tab-nav"> <li class="active">Tab1</li> <li>Tab2</li> <li>Tab3</li> <li>Tab4</li> </ul> <div class="tab-content"> <div class="tab-pane active">Tab1 Content</div> <div class="tab-pane">Tab2 Content</div> <div class="tab-pane">Tab3 Content</div> <div class="tab-pane">Tab4 Content</div> </div> </div> ``` CSS样式: ```css .tab-wrap { position: relative; } .tab-nav { display: flex; justify-content: space-between; } .tab-nav li { cursor: pointer; padding: 10px; font-size: 16px; font-weight: bold; border: 1px solid #ccc; border-bottom: none; } .tab-nav li.active, .tab-nav li:hover { background-color: #ccc; } .tab-content { position: absolute; top: 40px; left: 0; width: 100%; height: 200px; overflow: hidden; } .tab-pane { display: none; padding: 20px; } .tab-pane.active { display: block; } ``` JavaScript代码: ```javascript $(function() { // 获取tab标签和内容元素 var $tabNav = $(".tab-nav li"); var $tabContent = $(".tab-pane"); // 绑定tab标签点击事件 $tabNav.click(function() { // 获取点击的tab标签索引 var index = $(this).index(); // 切换tab标签的active类 $tabNav.removeClass("active"); $(this).addClass("active"); // 滑动切换tab内容 $tabContent.stop().animate({ "top": -index * $tabContent.height() }, 500); }); }); ``` 这段代码实现了以下功能: - 点击tab标签切换标签的active状态 - 滑动切换tab内容 注意:这只是一个简单的示例,具体实现方式可以根据需求进行调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值