11<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 >
710 </div >
811 </transition >
912</template >
@@ -18,18 +21,39 @@ export default {
1821 backPosition: {
1922 type: Number ,
2023 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'
2140 }
41+
2242 },
2343 data () {
2444 return {
25- visible: false
45+ visible: false ,
46+ interval: null
2647 }
2748 },
2849 mounted () {
29- window .addEventListener (' scroll' , this .handleScroll )
50+ window .addEventListener (' scroll' , this .handleScroll );
3051 },
3152 beforeDestroy () {
32- window .removeEventListener (' scroll' , this .handleScroll )
53+ window .removeEventListener (' scroll' , this .handleScroll );
54+ if (this .interval ) {
55+ clearInterval (this .interval );
56+ }
3357 },
3458 methods: {
3559 handleScroll () {
@@ -38,11 +62,11 @@ export default {
3862 backToTop () {
3963 const start = window .pageYOffset ;
4064 let i = 0 ;
41- const t = setInterval (() => {
65+ this . interval = setInterval (() => {
4266 const next = Math .floor (this .easeInOutQuad (10 * i, start, - start, 500 ));
4367 if (next <= this .backPosition ) {
4468 window .scrollTo (0 , this .backPosition );
45- clearInterval (t )
69+ clearInterval (this . interval )
4670 } else {
4771 window .scrollTo (0 , next);
4872 }
@@ -59,33 +83,28 @@ export default {
5983<style scoped>
6084.back-to-top {
6185 position : fixed ;
62- right : 50px ;
63- bottom : 50px ;
6486 display : inline-block ;
65- height : 40px ;
66- width : 40px ;
67- box-shadow : 1px 1px 1px #58B7FF ;
6887 text-align : center ;
6988 cursor : pointer ;
70- background : #58B7FF ;
71- color : #fff ;
89+ }
90+
91+ .back-to-top :hover {
92+ background : #d5dbe7 ;
7293}
7394
7495.fade-enter-active ,
7596.fade-leave-active {
76- transition : opacity 1 s
97+ transition : opacity .5 s ;
7798}
7899
79100.fade-enter ,
80101.fade-leave-to {
81102 opacity : 0
82103}
83104
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 ;
89108}
90109 </style >
91110
0 commit comments