1. 程式人生 > 其它 >關於移動端使用echarts點選圖示外部不能關閉tooltip的問題

關於移動端使用echarts點選圖示外部不能關閉tooltip的問題

新建一個mixin檔案  貼上如下程式碼:
 1 /**
 2  * 1. 需要將echart例項賦值為  this.echartsInstance  `echartsInstance`  echarts 帶s
 3  * 2. echarts所在的元件大小應該跟echarts圖表實際大小相差不多
 4  * 3. 引入即可,無需編寫其他程式碼
 5  */
 6  export const mixinAutoHideTooltip = {
 7     mounted() {
 8         this.mAutoHideTooltip(this.$el);
 9     },
10     data() {
11         this
.mIsSelfTouch = false; 12 return {}; 13 }, 14 beforeDestroy() { 15 this.mRemoveListeners(this.$el); 16 }, 17 methods: { 18 mAutoHideTooltip(dom) { 19 dom.addEventListener('touchstart', this.mHandleContainerTouchStart); 20 dom.addEventListener('touchend', this
.mHandleContainerTouchEnd); 21 document.addEventListener('touchstart', this.mHandleDomcumentTouchStart); 22 }, 23 mRemoveListeners(dom) { 24 dom.removeEventListener('touchstart', this.mHandleContainerTouchStart); 25 dom.removeEventListener('touchend', this
.mHandleContainerTouchEnd); 26 document.removeEventListener('touchstart', this.mHandleDomcumentTouchStart); 27 }, 28 mHandleContainerTouchStart() { 29 this.mIsSelfTouch = true; 30 }, 31 mHandleContainerTouchEnd() { 32 this.mIsSelfTouch = false; 33 }, 34 mHandleDomcumentTouchStart() { 35 if (this.mIsSelfTouch) return; 36 this.echartsInstance && this.mHideTooltip(this.echartsInstance); 37 }, 38 mHideTooltip(echartsInstance) { 39 echartsInstance.dispatchAction({ 40 type: 'updateAxisPointer', 41 currTrigger: 'mousemove', 42 x: 0, 43 y: 0 44 }); 45 } 46 } 47 };

使用時如下: