You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vite环境下Swiper的destroy方法失效问题求助

Vite环境下Swiper销毁方法失效问题

我希望在窗口宽度小于768px时销毁Swiper实例,但在Vite环境中调用destroy()方法后,仍能滑动Swiper,说明销毁操作未生效。相同代码在CodePen中可正常运行,但在Vite的项目示例里,当窗口宽度低于断点时Swiper并未被销毁。

以下是两段示例中一致的Swiper相关代码:

let mySwiper;

const wwEl = document.getElementById("ww");

function handleSwiper() {
  wwEl.textContent = `Window width: ${window.innerWidth}px`;

  if (window.innerWidth > 768) {
    mySwiper = new Swiper(".swiper", {
      spaceBetween: 10,
      slidesPerView: 3
    });
  } else {
    if (mySwiper) {
      mySwiper = mySwiper.destroy(true, true);
    }
  }
}

window.addEventListener("resize", handleSwiper, true);
handleSwiper();

内容的提问来源于stack exchange,提问作者Elvis Rauza

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 04:49:53