解决VantUI中Swiper内的滚动视图和Swiper滑动冲突问题

178 阅读1分钟

这个问题是在项目开发过程中遇到的,需求是外部轮播滑动,每一个滑动的 item 内部可能会存在横向超出屏幕的子视图,在滑动子视图的时候,和 Swiper 的滑动冲突了,导致内部的滑动视图无法滑动,直接滑动了外部的 Swiper 视图。

解决办法:
在子视图的 div 标签上加上 @touchstart.stop @touchmove.stop @touchend.stop,即 事件冒泡阻断。

代码如下:

<div @touchstart.stop @touchmove.stop @touchend.stop style="width: 300px; display: flex; overflow-x: auto;">
    <div style="flex-shrink: 0; width: 80px;">xxxx</div>
    <div style="flex-shrink: 0; width: 80px;">xxxx</div>
    <div style="flex-shrink: 0; width: 80px;">xxxx</div>
    <div style="flex-shrink: 0; width: 80px;">xxxx</div>
    <div style="flex-shrink: 0; width: 80px;">xxxx</div>
    <div style="flex-shrink: 0; width: 80px;">xxxx</div>
</div>

这样在滑动子区域的滑动视图时,就不会和 Swiper 的滑动冲突了。