echart / vue 实现自适应

方法一 elementResizeDetectorMaker

1.下载 yarn add elementResizeDetectorMaker
2.引入 import elementResizeDetectorMaker from “element-resize-detector”
3.使用
var dom = document.getElementById("container")

			//  随外层div的大小变化自适应
            const _this = this
            let erd = elementResizeDetectorMaker()
            erd.listenTo(dom, () => {
                _this.$nextTick(() => {
                    this.myChart1.resize()
                })
            })

方法二 window.onresize

mounted() {
        // 挂载window.onresize方法
        const that = this
        window.onresize = () => {
            // $(".online").width("25vw")
            // $(".online").height("21vw")
            // this.myChart2.resize()
            return (() => {
                window.screenWidth = document.body.clientWidth
                that.screenWidth = window.screenWidth
                let height1 = $(".report1").width()
                $(".report1").css("height", height1)
                let height2 = $(".report2").width()
                $(".report2").css("height", height2)
            })()
        }
      
    },

 watch: {
        screenWidth(val) {
            // 为了避免频繁触发resize函数导致页面卡顿,使用定时器
            if (!this.timer) {
                // 一旦监听到的screenWidth值改变,就将其重新赋给data里的screenWidth
                this.screenWidth = val
                this.timer = true

                setTimeout(() => {
                    // 处理宽度
                    this.timer = false
                    // console.log(this.screenWidth)
                }, 400)
            }
        },
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值