vue 浏览器宽度变化实时监听

监听页面宽度,调整显示样式

  data() {
    return {
      screenWidth: document.body.clientWidth
    }
  },
 watch: {
    screenWidth(val) {
      if (!this.timer) {
        this.screenWidth = val
        this.timer = true
        let that = this
        setTimeout(function() {
          console.log(that.screenWidth)
          that.timer = false
        }, 400)
      }
    }
  },
  mounted() {
    const that = this
    window.onresize = () => {
      return (() => {
        window.screenWidth = document.body.clientWidth
        that.screenWidth = window.screenWidth
      })()
    }
  },

你可能感兴趣的:(vue.js,javascript,前端,浏览器宽度,实时,样式兼容,容器)