0


Vue,js 监听window窗口尺寸变化

1.监听window窗口变化

VueJs 监听 window.resize 方法,同时窗口拉伸时会频繁触发resize函数,导致页面性能 卡顿 ,可以搭配setTimeout来提升性能

data() {
    return {
       screenWidth: document.body.clientWidth,//初始化宽度
    }
},

在mounted中挂载resize方法

var _this = this
window.onresize = () => {
        return (() => {
            _this .screenWidth = document.body.clientWidth
        })()
      }

watch 监听 data中或props传递的数据

screenWidth(){
        if (!this.timer) {
            this.timer = true
            let _this= this
            setTimeout(function () {
              ... (执行的语句)
              _this.timer = false
            }, 500)
        }
      }

本文转载自: https://blog.csdn.net/weixin_43003858/article/details/127527605
版权归原作者 草木疏 所有, 如有侵权,请联系我们删除。

“Vue,js 监听window窗口尺寸变化”的评论:

还没有评论