首先我们要获取页面的dom元素
ref="home"
this.$refs.home
<span class="hui" v-show="huiShow" @click="huiTop">回到顶部</span>
mounted() {
console.log(this.$refs.home);
this.$refs.home.addEventListener('scroll',this.handlerscroll)
},
destroyed(){
this.$refs.home.removeEventListener('scroll',this.handlerscroll)
},
methods:{
handlerscroll() {
console.log(Math.ceil(this.$refs.home.scrollTop));
if(Math.ceil(this.$refs.home.scrollTop) >= 100) {
this.huiShow = true
} else {
this.huiShow = false
}
},
huiTop() {
this.$refs.home.scroll({
top:0,
behavior:'smooth'
})
}
}