echarts 在 zoom下点击位置错位问题

1,539 阅读1分钟
  • 对于PC端需要整体缩放,适配大小屏的情况下,使用zoom,效果最好,最便捷
    const zoom = window.innerWidth < 1850 ? window.innerWidth / 1850 : 1
    document.body.style.zoom = zoom
  • 这种写法基本可以适配所有主流浏览器,但是如果使用了,echarts图表插件后,点击功能,或者hover之类的鼠标位置都会错位
  • 对于宽高是继承父级,100%, 50%, 这种写法的,只要把容器的zoom反向设置就可以了
    const zoom = window.innerWidth < 1850 ? window.innerWidth / 1850 : 1

    const MapContent = document.querySelector(id
    MapContent.style.zoom = 1 / zoom
    echarts.init()
  • 对于宽高是写死的, 如: height: 300px; width: 300px; 就需要同时缩放宽高
    const zoom = window.innerWidth < 1850 ? window.innerWidth / 1850 : 1
    const echartContent = document.querySelector(id)
    echartContent.style.zoom = 1 / zoom
    echartContent.style.width = echartContent.offsetWidth * zoom + 'px'
    echartContent.style.height = echartContent.offsetHeight * zoom + 'px'
    echarts.init()