前段时间做项目出现的问题
前提已引入swiper,此版本为原生js版,非jquery版
var swiper = new Swiper('.swiper-container', {
direction: 'vertical',
});Ï
var startScroll, touchStÏart, touchCurrent;
swiper.slides.on('touchstart', function (e) {
startScroll = this.scrollTop;
touchStart = e.targetTouches[0].pageY;
}, true);
swiper.slides.on('touchmove', function (e) {
touchCurrent = e.targetTouches[0].pageY;
var touchesDiff = touchCurrent - touchStart;
var slide = this;
var onlyScrolling =
(slide.scrollHeight > slide.offsetHeight) &&
(
(touchesDiff < 0 && startScroll === 0) ||
(touchesDiff > 0 && startScroll === (slide.scrollHeight - slide.offsetHeight)) ||
(startScroll > 0 && startScroll < (slide.scrollHeight - slide.offsetHeight))
);
if (onlyScrolling) {
e.stopPropagation();
}
}, true);
原理比较简单,主要是slide.scrollHeight > slide.offsetHeight判断实现
本文介绍了一个关于Swiper在垂直滚动模式下与其他触摸事件冲突的问题及解决方案。通过监听touchstart和touchmove事件,判断是否仅进行滚动操作,并阻止默认事件传播,确保了Swiper在触摸设备上的正常工作。
444

被折叠的 条评论
为什么被折叠?



