一、问题起因:
写uniapp的时候用到了ucharts的折线图,这个折线图我是放在component里面作为组件调用的,但是我调用的位置又是在swiper里面,那么问题来了,两个组件都具有横向的touchmove事件,这就需要使用到事件冒泡了,随后进入ucharts内修改源码
二、修复过程
<view
v-on:tap="rdcharts.tap"
v-on:mousemove="rdcharts.mouseMove"
v-on:mousedown="rdcharts.mouseDown"
v-on:mouseup="rdcharts.mouseUp"
v-on:touchstart="rdcharts.touchStart"
v-on:touchmove="rdcharts.touchMove"
v-on:touchend="rdcharts.touchEnd"
:id="'UC'+cid"
:prop="uchartsOpts"
:change:prop="rdcharts.ucinit"
>
<canvas
:id="cid"
:canvasId="cid"
:style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }"
:disable-scroll="disableScroll"
@error="_error"
v-show="showchart"
/>
</view>
这段就是ucharts中各类事件所在的div了,我首先是在touchmove上加了.stop.prevent但是发现就连uchart本身的事件都触发不了,随后我网上查找了一些资料,基本上都是加一个<scroll scroll-x:"true"></scroll>然后把要设置touchmove的div丢进去,我也试了试但是不行。(链接如下 链接如下)然后我就纳闷了很久,也没想到该怎么办。
三、解决方法
然后我就想既然是事件冒泡,那么就是一层层往上走的触发链,我需要保证我下层的盒子可以触发但是上层的盒子不能触发,那么最简单粗暴的就是中间加一层可以拦截该事件的盒子就行了,正好上述文献中提到需要加一个scroll-x的scroll盒子,于是就有了
<scroll-view scroll-x="true" v-on:touchmove.stop.prevent="rdcharts.touchMove">
<view
v-on:tap="rdcharts.tap"
v-on:mousemove="rdcharts.mouseMove"
v-on:mousedown="rdcharts.mouseDown"
v-on:mouseup="rdcharts.mouseUp"
v-on:touchstart="rdcharts.touchStart"
v-on:touchmove="rdcharts.touchMove"
v-on:touchend="rdcharts.touchEnd"
:id="'UC'+cid"
:prop="uchartsOpts"
:change:prop="rdcharts.ucinit"
>
<canvas
:id="cid"
:canvasId="cid"
:style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }"
:disable-scroll="disableScroll"
@error="_error"
v-show="showchart"
/>
</view>
</scroll-view>
上述代码中我只是加了一个scroll盒子并且复制了一遍touchmove事件,我将该事件在view触发后向上冒泡,然后遇到scroll的stop.prevent阻止冒泡,就可以截断触发链了,效果也是如预期中一样,希望该记录对各位有用。