echarts监听点击(空白处)事件,echarts监听滑动事件

本文详细介绍了如何使用ECharts的getZr()属性来监听和处理图表上的滑动和点击事件,通过具体代码示例展示了如何获取事件参数,并在'grid'区域中转换像素坐标到数据坐标。

利用getZr()属性获取滑动事件和点击事件,(需要支持es6,否则自行改写)

let obj = document.getElementById('xxxxxxxxxxxxxxId')
if (!obj) return
let myChart = echarts.init(obj)
myChart.setOption(item.option)
// 点击
myChart.getZr().on('click', params => {
  const pointInPixel = [params.offsetX, params.offsetY]
  if (myChart.containPixel('grid', pointInPixel)) {
    let xIndex = myChart.convertFromPixel({ seriesIndex: 0 }, [params.offsetX, params.offsetY])[0]
    alert(xIndex)
  }
})
// 滑动
myChart.getZr().on('mousemove', params => {
  const pointInPixel = [params.offsetX, params.offsetY]
  if (myChart.containPixel('grid', pointInPixel)) {
    let xIndex = myChart.convertFromPixel({ seriesIndex: 0 }, [params.offsetX, params.offsetY])[0]
    alert(xIndex)
  }
})

 

在使用 `uni-popup` 组件时,如果希望点击遮罩层(弹窗外部的空白区域)时不关闭弹窗,可以通过设置组件的属性来实现这一需求。默认情况下,`uni-popup` 在遮罩层被点击时会触发关闭弹窗的行为,但通过配置相关参数可以阻止此行为。 ### 阻止遮罩层点击关闭弹窗 `uni-popup` 提供了一个 `mask-click` 属性,用于控制是否允许点击遮罩层关闭弹出层。将该属性设置为 `false` 可以禁用遮罩层的点击关闭功能[^1]。因此,在组件标签中添加如下属性即可: ```html <uni-popup :mask-click="false" ref="popup" background-color="#fff" @change="change"> <!-- 弹窗内容 --> </uni-popup> ``` 通过这种方式设置后,用户点击遮罩层时将不再触发弹窗的关闭操作,从而满足特定场景下的交互需求。 ### 其他相关注意事项 - 如果还需要进一步控制遮罩层的行为,例如阻止触摸滑动事件穿透到页面其他部分,可以在组件上添加 `style="touch-action:none"` 来禁用某些手势操作[^1]。 - 除了 `mask-click` 外,`uni-popup` 还提供了许多其他配置选项,如动态控制弹窗打开和关闭的方法、监听弹窗状态变化的事件等,开发者可以根据实际需求灵活运用。 ### 示例代码 以下是一个完整的示例代码片段,展示了如何结合 `mask-click` 和手动控制弹窗开启与关闭的方法: ```html <template> <view> <button @click="openPopup">打开弹窗</button> <uni-popup :mask-click="false" ref="popup" background-color="#fff" @change="change" > <!-- 弹窗内容 --> <view style="padding: 20px;"> <text>这是一个不会因点击遮罩层而关闭的弹窗。</text> <button @click="closePopup">关闭弹窗</button> </view> </uni-popup> </view> </template> <script> import { ref } from 'vue'; export default { setup() { const popup = ref(null); const datetimePickerShow = ref(true); const openPopup = () => { popup.value?.open(); datetimePickerShow.value = false; }; const closePopup = () => { popup.value?.close(); datetimePickerShow.value = true; }; const change = (e) => { console.log('弹窗状态变化:', e.show); }; return { popup, openPopup, closePopup, change }; } }; </script> ``` 上述代码实现了点击按钮打开弹窗,并且即使点击了遮罩层也不会关闭弹窗的功能。同时,提供了一个“关闭弹窗”按钮供用户主动关闭弹窗。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值