Skip to content

Commit 5069a4d

Browse files
dongsuoPanJiaChen
authored andcommitted
'修改返回顶部功能'
1 parent bf08756 commit 5069a4d

File tree

2 files changed

+64
-30
lines changed

2 files changed

+64
-30
lines changed

src/components/BackToTop/index.vue

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
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 1s
97+
transition: opacity .5s;
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

src/views/components/backToTop.vue

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<template>
22
<div class="components-container">
33
<code>页面滚动到指定位置会在右下角出现返回顶部按钮</code>
4+
<code>可自定义按钮的样式、show/hide临界点、返回的位置
5+
如需文字提示,可在外部使用Element的el-tooltip元素 </code>
46
<div>我是占位</div>
57
<div>我是占位</div>
68
<div>我是占位</div>
@@ -125,19 +127,32 @@
125127
<div>我是占位</div>
126128
<div>我是占位</div>
127129
<div>我是占位</div>
128-
<back-to-top id="back-to-top-t" :visibilityHeight="300" :backPosition="50"></back-to-top>
130+
<!--可自定义按钮的样式、show/hide临界点、返回的位置 -->
131+
<!--如需文字提示,可在外部添加element的<el-tooltip></el-tooltip>元素 -->
132+
<el-tooltip placement="top" content="文字提示">
133+
<back-to-top transitionName="fade" :customStyle="myBackToTopStyle" :visibilityHeight="300" :backPosition="50"></back-to-top>
134+
</el-tooltip>
129135
</div>
130136
</template>
131137
<script>
132138
import BackToTop from 'components/BackToTop';
133139
export default {
134-
components: { BackToTop }
135-
140+
components: { BackToTop },
141+
data() {
142+
return {
143+
myBackToTopStyle: {
144+
right: '50px',
145+
bottom: '50px',
146+
width: '40px',
147+
height: '40px',
148+
'border-radius': '4px',
149+
'line-height': '40px', // 请保持与高度一致以垂直居中
150+
background: '#e7eaf1'// 按钮的背景颜色
151+
}
152+
}
153+
}
136154
}
137155
</script>
138156
<style scoped>
139-
#back-to-top-t{
140-
right: 100px;
141-
bottom: 100px;
142-
}
157+
143158
</style>

0 commit comments

Comments
 (0)