react 中 Swiper vertical 模式下 autoHeight 失效的问题

Swiper 在 vertical 模式下 autoHeight 失效的问题,导致页面出现多余的空白高度,网上找了很多方法都无效,我直接暴力更改。

javascript 复制代码
<Swiper
  className='index-swiper'
  direction={'vertical'}
  mousewheel={true}
  centeredSlides={true}
  autoHeight={true}
  slidesPerView="auto" // 这里 auto
  observeParents={true}
  observer={true}
  modules={[Mousewheel]}
  onSlideChange={(swiper) => {
    if(swiper.activeIndex == 5) { // 我的item有6条,取最后有问题的一条
        const dom = document.getElementsByClassName('swiper-wrapper')[0]; // 获取到 transform 的元素
        const y = (swiper.height - (swiper as any).slidesSizesGrid[swiper.activeIndex]) / 2 + 'px'; // 具体 translate 数值根据计算出来的结果而定。
        (dom as any).style.transform += `translateY(${y})` // 暴力更改
        
    }
  }}
>
  <SwiperSlide><Video></Video></SwiperSlide>
  <SwiperSlide><ModelDisplay></ModelDisplay></SwiperSlide>
  <SwiperSlide><Introduction></Introduction></SwiperSlide>
  <SwiperSlide><Appointment></Appointment></SwiperSlide>
  <SwiperSlide><ServiceSupport></ServiceSupport></SwiperSlide>
  <SwiperSlide><Footer></Footer></SwiperSlide>
</Swiper>
相关推荐
清汤饺子2 分钟前
OpenClaw 本地部署教程 - 从 0 到 1 跑通你的第一只龙虾
前端·javascript·vibecoding
爱吃的小肥羊2 小时前
比 Claude Code 便宜一半!Codex 国内部署使用教程,三种方法任选一!
前端
IT_陈寒4 小时前
SpringBoot项目启动慢?5个技巧让你的应用秒级响应!
前端·人工智能·后端
树上有只程序猿4 小时前
2026低代码选型指南,主流低代码开发平台排名出炉
前端·后端
橙某人4 小时前
LogicFlow 小地图性能优化:从「实时克隆」到「占位缩略块」!🚀
前端·javascript·vue.js
高端章鱼哥5 小时前
为什么说用OpenClaw对打工人来说“不划算”
前端·后端
大脸怪5 小时前
告别 F12!前端开发者必备:一键管理 localStorage / Cookie / SessionStorage 神器
前端·后端·浏览器
Mr_Mao5 小时前
我受够了混乱的 API 代码,所以我写了个框架
前端·api
小徐_23335 小时前
向日葵 x AI:把远程控制封装成 MCP,让 AI 替我远程控制设备
前端·人工智能
冴羽5 小时前
来自顶级大佬 TypeScript 之父的 7 个启示
前端·typescript