Skip to content

Commit 12ac041

Browse files
committed
feat(路由/滚动): 添加滚动到顶部功能
- 在路由配置中添加默认滚动到顶部的行为 - 创建新的ScrollToTop组件替换原有的back-to-top - 组件实现平滑滚动效果并优化移动端显示
1 parent 6c29170 commit 12ac041

4 files changed

Lines changed: 107 additions & 5 deletions

File tree

src/App.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,7 @@
190190
</div>
191191
</div>
192192
</footer>
193-
<back-to-top bottom="100px" right="50px">
194-
<button type="button" class="btn btn-light">
195-
<i class="fa fa-chevron-up"></i>
196-
</button>
197-
</back-to-top>
193+
<ScrollToTop />
198194
</div>
199195
</template>
200196

@@ -538,6 +534,7 @@ body,
538534
import $ from "jquery";
539535
import Identicon from "identicon.js";
540536
import { Hash } from "crypto";
537+
import ScrollToTop from './components/ScrollToTop.vue';
541538
export default {
542539
data() {
543540
return {
@@ -559,6 +556,9 @@ export default {
559556
this.$router.go(0);
560557
}
561558
},
559+
components: {
560+
ScrollToTop
561+
},
562562
computed: {
563563
name: function() {
564564
return this.$store.state.name;

src/components/ScrollToTop.vue

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<template>
2+
<div
3+
class="scroll-to-top"
4+
:class="{ 'show': isVisible }"
5+
@click="scrollToTop"
6+
>
7+
<i class="fa fa-arrow-up"></i>
8+
</div>
9+
</template>
10+
11+
<script>
12+
export default {
13+
name: 'ScrollToTop',
14+
data() {
15+
return {
16+
isVisible: false,
17+
scrollPosition: 0
18+
}
19+
},
20+
mounted() {
21+
// 监听滚动事件
22+
window.addEventListener('scroll', this.handleScroll)
23+
},
24+
beforeDestroy() {
25+
// 移除滚动事件监听
26+
window.removeEventListener('scroll', this.handleScroll)
27+
},
28+
methods: {
29+
handleScroll() {
30+
// 当页面滚动超过300px时显示按钮
31+
this.isVisible = window.pageYOffset > 300
32+
this.scrollPosition = window.pageYOffset
33+
},
34+
scrollToTop() {
35+
// 使用jQuery实现平滑滚动效果
36+
if (window.$) {
37+
$('html, body').animate({ scrollTop: 0 }, 500)
38+
} else {
39+
// 降级方案:不使用动画
40+
window.scrollTo({ top: 0, behavior: 'smooth' })
41+
}
42+
}
43+
}
44+
}
45+
</script>
46+
47+
<style scoped>
48+
.scroll-to-top {
49+
position: fixed;
50+
bottom: 30px;
51+
right: 30px;
52+
width: 40px;
53+
height: 40px;
54+
border-radius: 50%;
55+
background-color: white;
56+
color: black;
57+
border: 1px solid #ddd;
58+
display: flex;
59+
align-items: center;
60+
justify-content: center;
61+
cursor: pointer;
62+
z-index: 999;
63+
opacity: 0;
64+
visibility: hidden;
65+
transition: all 0.3s ease;
66+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
67+
}
68+
69+
.scroll-to-top.show {
70+
opacity: 1;
71+
visibility: visible;
72+
}
73+
74+
.scroll-to-top:hover {
75+
background-color: #f8f8f8;
76+
transform: translateY(-3px);
77+
}
78+
79+
.scroll-to-top i {
80+
font-size: 18px;
81+
}
82+
83+
@media (max-width: 768px) {
84+
.scroll-to-top {
85+
bottom: 20px;
86+
right: 20px;
87+
width: 35px;
88+
height: 35px;
89+
}
90+
91+
.scroll-to-top i {
92+
font-size: 16px;
93+
}
94+
}
95+
</style>

src/router.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ const router = new Router({
5858
base: "/",
5959
linkActiveClass: "active",
6060
linkExactActiveClass: "exact-active",
61+
scrollBehavior() {
62+
// 始终滚动到顶部
63+
return { x: 0, y: 0 }
64+
},
6165
routes: [
6266
// 主页
6367
{

src/views/Tech.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ export default {
188188
this.content = response.data
189189
this.currentPath = path
190190
191+
// 每次加载新文档时滚动到页面顶部
192+
window.scrollTo({ top: 0, behavior: 'smooth' })
193+
191194
// 获取文档标题(可能包含父菜单/子菜单格式)
192195
const docTitle = this.getDocTitle(path)
193196

0 commit comments

Comments
 (0)