美文网首页
vue中比较好用的监听窗口变化的指令

vue中比较好用的监听窗口变化的指令

作者: 墓寒丶 | 来源:发表于2021-07-29 10:26 被阅读0次
Vue.directive('resize', {
  // 使用全局注册指令的方式
  // 指令的名称
  bind(el, binding) {
     // el为绑定的元素,binding为绑定给指令的对象
    const interval = 500;// 间隔时间
    let width = "",
      height = "";
    function isReize() {
      const style = document.defaultView.getComputedStyle(el);
      if (width !== style.width || height !== style.height) {
        binding.value(); // 关键
      }
      width = style.width;
      height = style.height;
    }
    
    el.__vueSetInterval__ = setInterval(isReize, interval);
  },
  unbind(el) {
    clearInterval(el.__vueSetInterval__);
  },
})

相关文章

网友评论

      本文标题:vue中比较好用的监听窗口变化的指令

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