1
1
<template >
2
- <transition name =" fade" >
3
- <div class =" back-to-top" @click =" backToTop" v-show =" visible" >
4
- <el-tooltip class =" item" effect =" dark" content =" 返回顶部" placement =" top" >
5
- <i class =" el-icon-arrow-up" ></i >
6
- </el-tooltip >
2
+ <transition :name =" transitionName" >
3
+ <div class =" back-to-top" @click =" backToTop" v-show =" visible" :style =" customStyle" >
4
+ <svg width =" 16" height =" 16" viewBox =" 0 0 17 17" xmlns =" http://www.w3.org/2000/svg" class =" Icon Icon--backToTopArrow" aria-hidden =" true" style =" height : 16px ; width : 16px ;" >
5
+ <title >回到顶部</title >
6
+ <g >
7
+ <path d =" M12.036 15.59c0 .55-.453.995-.997.995H5.032c-.55 0-.997-.445-.997-.996V8.584H1.03c-1.1 0-1.36-.633-.578-1.416L7.33.29c.39-.39 1.026-.385 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.004z" fill-rule =" evenodd" ></path >
8
+ </g >
9
+ </svg >
7
10
</div >
8
11
</transition >
9
12
</template >
@@ -18,18 +21,39 @@ export default {
18
21
backPosition: {
19
22
type: Number ,
20
23
default: 0
24
+ },
25
+ customStyle: {
26
+ type: Object ,
27
+ default: {
28
+ right: ' 50px' ,
29
+ bottom: ' 50px' ,
30
+ width: ' 40px' ,
31
+ height: ' 40px' ,
32
+ ' border-radius' : ' 4px' ,
33
+ ' line-height' : ' 40px' ,
34
+ background: ' #e7eaf1'
35
+ }
36
+ },
37
+ transitionName: {
38
+ type: String ,
39
+ default: ' fade'
21
40
}
41
+
22
42
},
23
43
data () {
24
44
return {
25
- visible: false
45
+ visible: false ,
46
+ interval: null
26
47
}
27
48
},
28
49
mounted () {
29
- window .addEventListener (' scroll' , this .handleScroll )
50
+ window .addEventListener (' scroll' , this .handleScroll );
30
51
},
31
52
beforeDestroy () {
32
- window .removeEventListener (' scroll' , this .handleScroll )
53
+ window .removeEventListener (' scroll' , this .handleScroll );
54
+ if (this .interval ) {
55
+ clearInterval (this .interval );
56
+ }
33
57
},
34
58
methods: {
35
59
handleScroll () {
@@ -38,11 +62,11 @@ export default {
38
62
backToTop () {
39
63
const start = window .pageYOffset ;
40
64
let i = 0 ;
41
- const t = setInterval (() => {
65
+ this . interval = setInterval (() => {
42
66
const next = Math .floor (this .easeInOutQuad (10 * i, start, - start, 500 ));
43
67
if (next <= this .backPosition ) {
44
68
window .scrollTo (0 , this .backPosition );
45
- clearInterval (t )
69
+ clearInterval (this . interval )
46
70
} else {
47
71
window .scrollTo (0 , next);
48
72
}
@@ -59,33 +83,28 @@ export default {
59
83
<style scoped>
60
84
.back-to-top {
61
85
position : fixed ;
62
- right : 50px ;
63
- bottom : 50px ;
64
86
display : inline-block ;
65
- height : 40px ;
66
- width : 40px ;
67
- box-shadow : 1px 1px 1px #58B7FF ;
68
87
text-align : center ;
69
88
cursor : pointer ;
70
- background : #58B7FF ;
71
- color : #fff ;
89
+ }
90
+
91
+ .back-to-top :hover {
92
+ background : #d5dbe7 ;
72
93
}
73
94
74
95
.fade-enter-active ,
75
96
.fade-leave-active {
76
- transition : opacity 1 s
97
+ transition : opacity .5 s ;
77
98
}
78
99
79
100
.fade-enter ,
80
101
.fade-leave-to {
81
102
opacity : 0
82
103
}
83
104
84
- .back-to-top i {
85
- display : inline-block ;
86
- width : 100% ;
87
- height : 100% ;
88
- line-height : 40px ;
105
+ .back-to-top .Icon {
106
+ fill : #9aaabf ;
107
+ background : none ;
89
108
}
90
109
</style >
91
110
0 commit comments