You need to enable JavaScript to run this app. Swiper.js嵌套垂直滑动器快速滚动鼠标滚轮时跳过首尾子幻灯片问题
优惠活动
大模型
产品
解决方案
定价
更多

Swiper.js嵌套垂直滑动器快速滚动鼠标滚轮时跳过首尾子幻灯片问题

问题原因

快速滚动鼠标滚轮时,滚轮产生的连续动量事件会被父子两层Swiper同时捕获:父Swiper触发切换到第2个嵌套slide后,未消费完的滚轮事件会直接传递给子Swiper,触发子Swiper向下切换,导致第一个子slide被跳过,向上滚动时逻辑同理。

修复方案
  • 方案1:调整子Swiper的滚轮事件释放规则
    给嵌套的子Swiper开启releaseOnEdges配置,仅当子Swiper滚动到自身的首尾边缘时,才将滚轮事件释放给父Swiper,避免事件透传。
  • 方案2:调整滚动灵敏度和动量参数
    原配置的freeModeMomentumRatio为10,数值过大会放大滚动动量,导致一次滚动触发多次切换,建议调整为1以下。同时给父Swiper增加threshold配置,设置滚轮滚动的最小触发距离,避免误触发,且将子Swiper的滚轮灵敏度设置为高于父Swiper,保证子层级优先响应滚轮事件。
  • 方案3:增加父Swiper切换防抖逻辑
    监听父Swiper的slideChange事件,当切换到嵌套slide所在的索引时,短时间内禁用父Swiper的滚轮响应,等子Swiper消费完当前的滚轮事件后再恢复。
修复后核心代码示例

JS部分

var swiperH = new Swiper('.swiper-container-main', {
    pagination: {
        el: '.swiper-pagination-main',
    },
    paginationClickable: true,
    direction: 'vertical',
    spaceBetween: 50,
    mousewheel: {
      invert: false,
      threshold: 80, // 新增:滚轮触发阈值,滚动距离达到设定值才触发切换
    },
    mousewheelForceToAxis: true,
    freeMode: false,
    mousewheelSensitivity: 0.3, // 调低父级灵敏度
    freeModeMomentumRatio: 1, // 调低动量放大系数,避免滚动惯性过大
    freeModeMinimumVelocity: 0.02,
    freeModeSticky: false
});
var swiperV = new Swiper('.swiper-container-inner', {
    pagination: {
        el: '.swiper-pagination-inner',
    },
    paginationClickable: true,
    direction: 'vertical',
    spaceBetween: 50,
    nested: true,
    mousewheel: {
      invert: false,
      releaseOnEdges: true, // 新增:仅当子Swiper滚到首尾边缘时,才把事件传递给父Swiper
    },
    effect: 'fade',
    fadeEffect: {
      crossFade: true
    },
    mousewheelForceToAxis: true,
    freeMode: false,
    mousewheelSensitivity: 0.6, // 子级灵敏度高于父级,优先响应滚轮操作
    freeModeMomentumRatio: 1,
    freeModeMinimumVelocity: 0.01,
    freeModeSticky: false
});

内容的提问来源于stack exchange,提问作者Muhammad Jamil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 09:15:02