/
our-gotop.vue
148 lines (140 loc) · 2.97 KB
/
our-gotop.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<template>
<transition name="fade">
<view
v-show="isShow"
@click="goTop"
:style="{
width: `${width}px`,
height: `${height}px`,
right: `${right}px`,
bottom: `${bottom}px`,
zIndex: zIndex
}"
class="gotop">
<slot>
<view>
<svg t="1587113576016" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2222" :width="width" :height="height">
<path :fill="color" d="M523.955 297.952l323.744 512c6.24 9.856 6.624 22.336 0.992 32.512-5.632 10.24-16.384 16.576-28.033 16.576l-647.328 0c-11.647 0-22.4-6.336-28.033-16.576-2.656-4.8-3.968-10.112-3.968-15.424 0-5.952 1.664-11.904 4.929-17.088l323.616-512c5.856-9.28 16.064-14.911 27.04-14.912s21.184 5.632 27.04 14.912z" p-id="2223"></path>
</svg>
</view>
</slot>
</view>
</view>
</transition>
</template>
<script>
export default {
name: 'ourGoTop',
data () {
return {
timer: null,
isShow: false
}
},
props: {
width: {
type: Number,
default: 50
},
height: {
type: Number,
default: 50
},
right: {
type: Number,
default: 30
},
bottom: {
type: Number,
default: 50
},
zIndex: {
type: Number,
default: 1000
},
color: {
type: String,
default: '#333'
},
scrollY: {
type: Number,
default: 1000
},
scrollSpeed: {
type: Number,
default: 100
},
duration: {
type: Number,
default: 300
}
},
mounted () {
//#ifndef H5
this.isShow = true
//#endif
//#ifdef H5
window.addEventListener('scroll', this.scrollAnimate)
//#endif
},
beforeDestroy () {
//#ifdef H5
window.removeEventListener('scroll', this.scrollAnimate)
//#endif
},
methods: {
//#ifndef H5
goTop () {
uni.pageScrollTo({
scrollTop: 0,
duration: this.duration
});
}
//#endif
//#ifdef H5
scrollAnimate () {
if (window.scrollY >= this.scrollY && this.isShow === false) {
this.isShow = true
} else if (window.scrollY < this.scrollY && this.isShow === true) {
this.isShow = false
}
},
goTop () {
let self = this
if (self.timer) {
cancelAnimationFrame(self.timer);
}
self.timer = requestAnimationFrame(function fn(){
let oTop = document.body.scrollTop || document.documentElement.scrollTop;
if(oTop > 0){
document.body.scrollTop = document.documentElement.scrollTop = oTop - self.scrollSpeed;
self.timer = requestAnimationFrame(fn);
}else{
cancelAnimationFrame(self.timer);
}
})
}
//#endif
}
}
</script>
<style scoped>
.gotop {
position: fixed;
display: flex;
justify-content: center;
align-items: center;
}
.fade-enter,
.fade-leave-to {
opacity: 0;
}
.fade-enter-to,
.fade-leave {
opacity: 1;
}
.fade-enter-active,
.fade-leave-active {
transition: 0.5s all linear
}
</style>