请添加图片描述

directives: {  // 使用局部注册指令的方式
    resize: { // 指令的名称
      bind (el, binding) { // el为绑定的元素,binding为绑定给指令的对象
        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, 300);
      },
      unbind (el) {
        clearInterval(el.__vueSetInterval__);
      }
    }
  },
methods:{
	   // 元素发生变化后执行
    resize () {
      console.log('我变了')
    },
}
template

  <div class="shichang borderColor"
                   v-resize="resize"
                   style="clear:both;margin-top:16px;">
	要监控的区域
</div>
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐