vue自定义指令

回到顶部                      js文件

export default {
install (Vue) {
Vue.directive('back-top', {
inserted (el, binding) {
let e = binding.arg || 'click'
el.addEventListener(e, () => {
var top = document.body.scrollTop
var timer = setInterval(() => {
top -= 50
if (top <= 0) {
top = 0
clearInterval(timer)
}
document.documentElement.scrollTop = document.body.scrollTop = top
}, 20)
})
}
})
}
}

  在mainjs引入       Vue.use('name')

原文地址:https://www.cnblogs.com/dujunfeng/p/8310914.html