vue自定义指令v-scroll
用于下拉加载更多
import Vue from 'vue';
Vue.directive("scroll", {
inserted: function(el, bind, vnode) {
let scrollHeight = el.offsetHeight,
cb_name = bind.expression, // 绑定的函数名
cb = vnode.context[cb_name];
el.addEventListener("scroll", async () => {
// +1防止高度不够
if (scrollHeight + el.scrollTop + 1 >= el.firstChild.clientHeight) {
try {
cb && (await cb()); // 触发绑定的函数
} catch (error) {
console.log(error);
}
}
});
}
});
使用