我是靠谱客的博主 碧蓝小熊猫,这篇文章主要介绍vue 之返回顶部组件,现在分享给大家,希望可以做个参考。

vue 之返回顶部组件

backTop.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<template> <div class="floating" @click="goTop()" v-show="btnFlag"> <a href="javascript:;"> <em class="icon-text">回顶部</em> </a> </div> </template> <script> export default { props: ["refEle", "backTime"], data() { return { btnFlag: false, }; }, mounted() { window.addEventListener("scroll", this.isShowBtn, true); }, destroyed() { window.removeEventListener("scroll", this.isShowBtn, true); }, methods: { goTop() { // 拿到当前 需要滚动的节点 距离顶部多少距离 // console.log('ref节点',this.refEle,this.refEle.scrollTop); this.timer = setInterval(() => { let scrollTop = this.refEle.scrollTop; let ispeed = Math.floor(-scrollTop / 5); // console.log("速度和高度", ispeed, scrollTop); this.refEle.scrollTop = scrollTop + ispeed; if (scrollTop === 0) { // console.log("到0了"); clearInterval(this.timer); } }, this.backTime); }, isShowBtn() { let scrollTop = ""; if (this.refEle) { scrollTop = this.refEle.scrollTop; } else { scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; } if (scrollTop > 60) { this.btnFlag = true; } else { this.btnFlag = false; } }, }, }; </script> <style scoped> .floating { width: 0.65rem; position: fixed; z-index: 1600; right: 0.2rem; bottom: 1rem; } .floating a { text-decoration: none; margin-bottom: 0.05rem; display: block; opacity: 0.9; background-color: rgb(130, 130, 130); } .floating a:hover { background-color: #33AAFF; } </style>

home.vue引用

复制代码
1
2
3
4
5
6
7
8
// 这个需要返回顶部的节点 <main ref="selfCon></main> <BackTop class="back_top" :refEle="this.$refs.selfCon" :backTime="30" ></BackTop>

最后

以上就是碧蓝小熊猫最近收集整理的关于vue 之返回顶部组件的全部内容,更多相关vue内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部