首页 > 技术文章 > vue自定义指令

dujunfeng 2018-01-18 16:35 原文

回到顶部                      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')

推荐阅读