Swiper在window触摸平板上不能使用的临时解决方案

在vue3工程中引入tailwindcss

Swiper在window触摸平板上不能使用的临时解决方案

20th Otc 2020

swiper问题解决

最近在做华为大屏的应用适配,发现swiper在window的触摸设备上无法使用(touch事件处理有问题),查询原因,发现swiper源码对window触摸屏的判断有问题,从而导致swiper不支持window触摸设备操作。

解决方案如下:

// 检测设备是否支持触摸
function touchCapable() {
    return (
      'ontouchstart' in window||
      (window.DocumentTouch && document instanceof window.DocumentTouch) ||
      navigator.maxTouchPoints > 0 ||
      window.navigator.msMaxTouchPoints > 0
    );
  };

// new Swiper的配置项,增加simulate选项
swiperConfig = {
 simulateTouch : !touchCapable()
}

如果在window触摸屏幕上,滑动到边界触发了其他行为(如触发了浏览器的前进后退),可以添加一个css属性禁止此行为:

html, body { overscroll-behavior: none; }
转载请注明出处:http://www.how2js.cn