监听尺寸元素变化

Vue.directive('resize', {
    bind(el, binding) {
      let width = '', height = '';
      function get() {
        const style = document.defaultView.getComputedStyle(el);
        if (width !== style.width || height !== style.height) {
          binding.value({width, height});
        }
        width = style.width;
        height = style.height;
      }

      el.__vueReize__ = setInterval(get, 200);
    },
    unbind(el) {
      clearInterval(el.__vueReize__);
    },
  });



<div id="canvas-wrap" v-resize="redraw">
    <canvas></canvas>
</div>
 
posted @ 2019-10-29 09:50  不再犯错  阅读(4327)  评论(0编辑  收藏  举报