文章目录
实现滚动页面的时候,显示侧边固定定位的top,点击top实现动画向上滚动到顶部
话不多说,直接上代码
<a class="scroll-to-top" @click="toTop">
<i class="fa fa-angle-double-up">i>
a>
//样式可以自己写
<script>
export default {
data() {
return {
i: 0
}
},
mounted() {
window.addEventListener('scroll', this.handleScroll, true)
},
methods: {
handleScroll() {
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
const scrollBox = document.querySelector('.scroll-to-top')
scrollTop > 300 ? scrollBox.style.opacity = 1 : scrollBox.style.opacity = 0
this.i = scrollTop
},
toTop() {
let top = document.documentElement.scrollTop || document.body.scrollTop
const timeTop = setInterval(() => {
document.body.scrollTop = document.documentElement.scrollTop = top -= 50
if (top <= 0) {
clearInterval(timeTop)
}
}, 10)
}
}
}
script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37