swiper 做的tab选项卡

本文介绍了一种利用Swiper库实现网页中不同内容区域平滑切换的方法,通过结合jQuery和Swiper,实现了导航菜单与内容区联动的效果,使得用户在不同页面间切换时体验更加流畅。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.2/css/swiper.min.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.2/js/swiper.min.js"></script>
  <script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.js"></script>
  <style>
    *{
      margin: 0;padding: 0;
    }
    ul{
      overflow: hidden;
    }
    li,ul{
      list-style: none;
    }
    li{
      float: left;
      width: 33%;
      height: 50px;
      line-height: 50px;
      text-align: center;
    }
    .active{
      color: red;
    }
    .swiper-container{
      height: calc(100vh - 50px);
    }
    .swiper-slide{
      overflow-y: scroll;
    }
  </style>
</head>
<body>
  <ul>
    <li class="active">首页</li>
    <li>发现</li>
    <li>用户</li>
  </ul>
  <div class="swiper-container">
    <div class="swiper-wrapper">
        <div class="swiper-slide" style="background: red">
            <p style="height:50px">1</p>
            <p style="height:50px">1</p>
            <p style="height:50px">1</p>
            <p style="height:50px">1</p>
            <p style="height:50px">1</p>
            <p style="height:50px">1</p>
            <p style="height:50px">1</p>
            <p style="height:50px">1</p>
            <p style="height:50px">1</p>
            <p style="height:50px">1</p>
            <p style="height:50px">1</p>
            <p style="height:50px">1</p>
        </div>
        <div class="swiper-slide" style="background: yellow">
           <p style="height:50px">2</p>
           <p style="height:50px">2</p>
           <p style="height:50px">2</p>
           <p style="height:50px">2</p>
           <p style="height:50px">2</p>
           <p style="height:50px">2</p>
           <p style="height:50px">2</p>
           <p style="height:50px">2</p>
        </div>
        <div class="swiper-slide" style="background: pink">
          <p style="height:50px">3</p>
          <p style="height:50px">3</p>
          <p style="height:50px">3</p>
        </div>
    </div>
  </div>
  <script>
      $('li').each(function(e) {
        var index = $(this).index();
        $(this).click(function() {
          $(this).addClass('active').siblings().removeClass('active')
          mySwiper.slideTo(index);
        })
      })

      var mySwiper = new Swiper ('.swiper-container', {
        loop: false, // 循环模式选项
        on: {
          slideChange: function () {
             $('li').eq(this.activeIndex).addClass('active').siblings().removeClass('active')
          },
        }
      })
  </script>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值