swiper结合gsap进行切换

网页滚动到mySwiper时,启用gsap,固定轮播图,当切换完后,继续进行网页滚动

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <title>Swiper demo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1" />
  <!-- Link Swiper's CSS -->
  <link rel="stylesheet" href="../css/swiper-bundle.min.css" />

  <!-- Demo styles -->
  <style>
    html,
    body {
      position: relative;
      height: 100%;
    }

    body {
      background: #eee;
      font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
      font-size: 14px;
      color: #000;
      margin: 0;
      padding: 0;
    }

    .swiper {
      width: 100%;
      height: 100%;
    }

    .swiper-slide {
      text-align: center;
      font-size: 18px;
      background: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .swiper-slide img {
      display: block;
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  </style>
</head>

<body>
  <div style="background: #957444;height: 100vh;"></div>
  <!-- Swiper -->
  <div class="swiper mySwiper">
    <div class="swiper-wrapper">
      <div class="swiper-slide">Slide 1</div>
      <div class="swiper-slide">Slide 2</div>
      <div class="swiper-slide">Slide 3</div>
      <div class="swiper-slide">Slide 4</div>
      <div class="swiper-slide">Slide 5</div>
    </div>
  </div>
  <div style="background: #000;height: 100vh;"></div>
  <!-- Swiper JS -->
  <script src="../js/swiper-bundle.min.js"></script>

  <!-- Initialize Swiper -->

  <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.9.1/gsap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.9.1/ScrollTrigger.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.9.1/ScrollToPlugin.min.js"></script>
  <script>
    // 防抖函数
    function debounce(func, wait) {
      let timeout;
      return function () {
        const context = this;
        const args = arguments;
        clearTimeout(timeout);
        timeout = setTimeout(function () {
          func.apply(context, args);
        }, wait);
      };
    }

    // 初始化Swiper
    var mySwiper = new Swiper(".mySwiper", {});

    // 注册gsap插件
    gsap.registerPlugin(ScrollTrigger);

    let currentIndex = 0;
    const totalImages = 5;
    const vhUnit = window.innerHeight; // 获取视口高度
    const endValue = totalImages * vhUnit;

    // 创建防抖后的onUpdate函数
    const debouncedOnUpdate = debounce(function (self) {
      const progress = self.progress;
      let newIndex = Math.floor(progress * totalImages);
      newIndex = Math.min(newIndex, totalImages - 1);
      newIndex = Math.max(newIndex, 0);

      if (newIndex !== currentIndex) {
        currentIndex = newIndex;
        mySwiper.slideTo(currentIndex, 1000, false);
      }
    }, 100); // 100毫秒的延迟,可以根据需要调整

    // 创建动画的时间轴,使用防抖后的onUpdate
    ScrollTrigger.create({
      trigger: ".mySwiper",
      start: 'top top',
      end: () => endValue,
      pin: true,
      scrub: true,
      onUpdate: debouncedOnUpdate // 使用防抖后的onUpdate
    });
  </script>
</body>

</html>

参考:https://blog.csdn.net/weixin_60886119/article/details/133132345

相关推荐

  1. swiper结合gsap进行切换

    2024-07-13 19:52:03       20 阅读
  2. swiper/vue踩坑 切换问题

    2024-07-13 19:52:03       29 阅读
  3. linux进程切换

    2024-07-13 19:52:03       39 阅读
  4. GSAP教程之gsap.config与gsap.defaults详解

    2024-07-13 19:52:03       31 阅读
  5. GSAP动画学习

    2024-07-13 19:52:03       36 阅读
  6. 初学gsap的记录

    2024-07-13 19:52:03       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 19:52:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 19:52:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 19:52:03       58 阅读
  4. Python语言-面向对象

    2024-07-13 19:52:03       69 阅读

热门阅读

  1. 昇思训练营打卡第二十四天(LSTM+CRF序列标注)

    2024-07-13 19:52:03       17 阅读
  2. Nginx 日志统计分析命令

    2024-07-13 19:52:03       22 阅读
  3. 天童美语:放假给孩子看什么地理纪录片

    2024-07-13 19:52:03       17 阅读
  4. Perl 语言开发(十三):网络编程

    2024-07-13 19:52:03       22 阅读
  5. 块设备驱动实现--模拟一个块设备

    2024-07-13 19:52:03       17 阅读
  6. Docker

    2024-07-13 19:52:03       15 阅读
  7. docker

    2024-07-13 19:52:03       21 阅读
  8. qint64 pendingDatagramSize() const;

    2024-07-13 19:52:03       20 阅读
  9. ThreadLocal有哪些应用场景?底层如何实现?

    2024-07-13 19:52:03       21 阅读
  10. IPython:提升Python编程效率的实用技巧与案例

    2024-07-13 19:52:03       19 阅读
  11. 赋值运算符.二

    2024-07-13 19:52:03       18 阅读
  12. 数据结构第25节 深度优先搜索

    2024-07-13 19:52:03       16 阅读
  13. Python面试题:如何在 Python 中发送 HTTP 请求?

    2024-07-13 19:52:03       18 阅读
  14. ThreadLocal使用的场景有哪些?

    2024-07-13 19:52:03       18 阅读