ref用于下面获取dom节点
<html ref='home'>
<span class="topBtn" v-show="topShow" @click="topHui">回到顶部</span>
</html>
mounted() {
获取上面的html元素refs是vue提供的获取元素的方法
this.$refs.home.addEventListener('scroll',this.handlerscroll)
},
destroyed(){
解绑html元素
this.$refs.home.removeEventListener('scroll',this.handlerscroll)
},
methods: {
topHui() {
this.$refs.home.scroll({
top:0,
behavior:'smooth'
})
},
handlerscroll() {
if(Math.ceil(this.$refs.home.scrollTop) >= 100) {
this.topShow = true
} else {
this.topShow = false
}
}