美文网首页
Vue监听窗口变化

Vue监听窗口变化

作者: 六寸光阴丶 | 来源:发表于2021-06-07 06:15 被阅读0次

监听resize事件,此事件将在窗口大小变化时触发,在事件回调中使用body.getBoundingClientRect()方法获取react,并使用它的width属性获取窗口的宽度,根据窗口宽度进行不同的操作,为了回调在初始化时立即触发一次,我们在监听事件之前先执行一次回调。

const { body } = document

export default {
  beforeMount () {
    this.$_resizeHandler()
    window.addEventListener('resize', this.$_resizeHandler)
  },
  methods: {
    $_resizeHandler () {
      const rect = body.getBoundingClientRect()
      console.log(rect.width)
    }
  }
}

相关文章

网友评论

      本文标题:Vue监听窗口变化

      本文链接:https://www.haomeiwen.com/subject/wmhzsltx.html