前言
本文主要介绍了Vue 实现返回顶部按钮的方法,下面话不多说,来直接看代码吧
实例代码:
复制代码
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61<template> <div class="scrollTop"> <div class="backTop" @click="backTop"> <button v-show="flag_scroll"> 返回顶部 </button> </div> //数据源 <div></div> </div> </template> <script> export default { name: 'scrollTop', data() { return { flag_scroll: false, scroll: 0, } }, computed: {}, methods: { //返回顶部事件 backTop() { document.getElementsByClassName('scrollTop')[0].scrollTop = 0 }, //滑动超过200时显示按钮 handleScroll() { let scrollTop = document.getElementsByClassName('scrollTop')[0] .scrollTop console.log(scrollTop) if (scrollTop > 200) { this.flag_scroll = true } else { this.flag_scroll = false } }, }, mounted() { window.addEventListener('scroll', this.handleScroll, true) }, created() { }, } </script> <style scoped> .scrollTop{ width: 100%; height: 100vh; overflow-y: scroll; } .backTop { position: fixed; bottom: 50px; z-index: 100; right: 0; background: white; } </style>
总结
到此这篇关于Vue实现返回顶部按钮的文章就介绍到这了,更多相关Vue返回顶部按钮内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!
最后
以上就是碧蓝纸鹤最近收集整理的关于Vue实现返回顶部按钮实例代码的全部内容,更多相关Vue实现返回顶部按钮实例代码内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复