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

2021-07-29  本文已影响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__);
  },
})
上一篇下一篇

猜你喜欢

热点阅读