样式如图 1:在components文件夹下新建BackTop.vue
<template>
<div class="backTopBtn">
<a href="javascript:;">
<div v-if="btnFlag" class="btn" @click="backTop">TOP</div>
</a>
</div>
</template>
<script>
export default {
data() {
return {
btnFlag: false,//控制top按钮是否显示
scroll: null//距离顶部的高度
}
},
mounted() {
window.addEventListener('scroll', this.scrollToTop)//监听浏览器滚动条,并执行this.scrollTop方法
},
destroyed() {
window.removeEventListener('scroll', this.scrollToTop)
},
methods: {
// 算距离顶部的高度,当高度大于600显示回顶部图标,小于60则隐藏
scrollToTop() {
this.scroll = document.documentElement.scrollTop || document.body.scrollTop;
if (this.scroll > 600) {
this.btnFlag = true
} else {
this.btnFlag = false
}
},
// 点击图片回到顶部方法,加计时器是为了过渡顺滑
backTop() {
let timer = setInterval(() => {
let ispeed = Math.floor(-this.scroll / 5)
document.documentElement.scrollTop = document.body.scrollTop = this.scroll + ispeed
if (this.scroll === 0) {
clearInterval(timer)
}
}, 16)
}
}
}
</script>
<style lang="scss">
.backTopBtn {
position: fixed;
right: 8px;
bottom: 30px;
background-color: rgb(78, 78, 78);
font-size: 18px;
border-radius: 10px;
a {
color: white;
}
.btn {
padding: 17px 10px;
}
}
.backTopBtn:hover {
a {
color: orange;
}
}
</style>
2:在需要使用的页面
<BackTop></BackTop>
import BackTop from "@/components/BackTop.vue";
export default {
components: {
BackTop
},
}