[前端] JavaScript实现Tab点击切换

1846 0
王子 2022-10-21 15:40:01 | 显示全部楼层 |阅读模式
Tab 选项卡切换效果在现如今的网页中,运用的也是比较多的,包括点击切换、滑动切换、延迟切换、自动切换等多种效果,在这篇博文里,我们是通过原生 Jquery 来实现 Tab 点击切换的效果
例如:


html
  1. <body>
  2.   <div style="position: relative;margin-top: 56px;display: flex;justify-content: space-between;width: 1200px;" id="left">
  3.     <ul class="gw_b_tab">
  4.       <li class="gw_b_item gw_b_item2">111</li>
  5.       <li class="gw_b_item">222</li>
  6.       <li class="gw_b_item">333</li>
  7.       <li class="gw_b_item">444</li>
  8.       <li class="gw_b_item">555</li>
  9.       <li class="gw_b_item">666</li>
  10.     </ul>
  11.     <div>
  12.       <div class="gw_b_box" style="display: block;">
  13.         <h3 class="gw_box_tit">111</h3>
  14.       </div>
  15.       <div class="gw_b_box" >
  16.         <h3 class="gw_box_tit">222</h3>
  17.       </div>
  18.       <div class="gw_b_box">
  19.         <h3 class="gw_box_tit">333</h3>
  20.       </div>
  21.       <div class="gw_b_box" >
  22.         <h3 class="gw_box_tit">444</h3>
  23.       </div>
  24.       <div class="gw_b_box">
  25.         <h3 class="gw_box_tit">555</h3>
  26.       </div>
  27.       <div class="gw_b_bo" >
  28.         <h3 class="gw_box_tit">666</h3>
  29.       </div>
  30.     </div>
  31. </div>
复制代码
css
  1. *{
  2.       padding: 0;
  3.       margin: 0;
  4.     }
  5.     li{
  6.       list-style: none;
  7.     }
  8.     .gw_b_tab{
  9. color: #333333;
  10. width: 240px;
  11. height: 490px;
  12. background: #FFFFFF;
  13. border-radius: 8px;
  14. overflow: hidden;
  15. overflow-y: hidden;
  16. }
  17. .gw_b_item{
  18. height: 82px;
  19.   padding: 0 22px;
  20.   position: relative;
  21.   font-size: 16px;
  22.   line-height: 80px;
  23. text-align: center;
  24.   cursor: pointer;
  25. border-radius: 8px;
  26.   font-weight: bold;
  27.   white-space: nowrap;
  28. }
  29. .gw_b_box{
  30. /* display: none; */
  31.   width: 880px;
  32. position: absolute;
  33. right: 0;
  34. top: 0;
  35.   display: none;
  36.   background: #FB6638;
  37.   height: 490px;
  38.   border-radius: 30px;
  39. }
  40. .gw_b_item2{
  41. background-color: #FB6638;
  42. color: #fff;
  43. }
  44. .gw_box_tit{
  45. height: 48px;
  46.   line-height: 48px;
  47.   font-size: 24px;
  48.   color: #ffffff;
  49.   margin-bottom: 18px;
  50.   text-align: center;
  51. }
复制代码
js
  1. <script src="/static/js/jquery-1.11.3.min.js"></script>
  2. <script>
  3.   $(function () {
  4.     $("#left .gw_b_item").click(function () {
  5.       var index = $(this).index();
  6.       $(".gw_b_box").eq(index).show();
  7.       $(this).addClass('gw_b_item2')
  8.       // :eq() 选择器选取带有指定 index 值的元素。
  9.       $(".gw_b_box").eq(index).siblings().hide();
  10.       $(this).siblings().removeClass('gw_b_item2')
  11.     })
  12.   })
  13. </script>
复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中国红客联盟。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行