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__);
},
})
网友评论