Skip to content

Commit f201b60

Browse files
committed
scrollbehave
1 parent 1fec084 commit f201b60

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

vue-noob-router/src/App.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ export default {
1515
</script>
1616

1717
<style>
18+
::-webkit-scrollbar {
19+
width: 0;
20+
}
1821
</style>

vue-noob-router/src/components/layouts/Navbar.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
<li class="nav-item">
66
<router-link active-class="active" exact to="/" class="btn btn-sm btn-info mx-2">Home</router-link>
77
</li>
8+
<li class="nav-item">
9+
<router-link
10+
active-class="active"
11+
:to="{name: 'About', hash: `#${index}`}"
12+
class="btn btn-sm btn-info mx-2"
13+
>About</router-link>
14+
</li>
815
<li class="nav-item">
916
<router-link active-class="active" to="/user" class="btn btn-sm btn-info mx-2">User</router-link>
1017
</li>
@@ -19,6 +26,16 @@
1926
<script>
2027
export default {
2128
name: "AppNavbar",
29+
data() {
30+
return {
31+
index: 3,
32+
};
33+
},
34+
watch: {
35+
$route() {
36+
this.index = Math.floor(Math.random() * 3) + 1;
37+
},
38+
},
2239
};
2340
</script>
2441

vue-noob-router/src/components/pages/About.vue

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<template>
2-
<div class="container center">
3-
<h1 class="light-green-text">About Page</h1>
4-
<button @click="goToHome" class="btn">Go to Home</button>
2+
<div>
3+
<div v-for="i in 3" :key="i" :id="i" class="container my-2">
4+
<h3 class="text-danger">About {{ i }}</h3>
5+
<p
6+
class="lead"
7+
>Lorem ipsum dolor sit amet consectetur adipisicing elit. Sit aliquid consectetur sint numquam voluptatem. Labore corporis nulla dolorum fugiat quaerat earum delectus eaque harum, debitis, velit aliquid distinctio, eveniet eum deleniti repellendus reiciendis vel! Laboriosam repudiandae ab ullam reprehenderit quidem.</p>
8+
</div>
59
</div>
610
</template>
711

@@ -16,5 +20,10 @@ export default {
1620
};
1721
</script>
1822

19-
<style>
23+
<style scoped>
24+
.container {
25+
height: 60vh;
26+
padding: 1rem;
27+
background-color: aliceblue;
28+
}
2029
</style>

vue-noob-router/src/main.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ Vue.use(VueRouter);
99
const router = new VueRouter({
1010
routes,
1111
mode: 'history',
12+
scrollBehavior(to, from, savedPosition) {
13+
if (savedPosition) {
14+
return savedPosition;
15+
}
16+
if (to.hash) {
17+
return {
18+
selector: to.hash,
19+
};
20+
}
21+
},
1222
});
1323

1424
new Vue({

vue-noob-router/src/routes.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Home from '@/components/pages/Home.vue';
2-
// import About from '@/components/pages/About.vue';
2+
import About from '@/components/pages/About.vue';
33
import User from '@/components/pages/User.vue';
44
import UserStart from '@/components/User/UserStart.vue';
55
import UserDetail from '@/components/User/UserDetail.vue';
@@ -9,13 +9,20 @@ import NotFound from '@/components/pages/404.vue';
99
export const routes = [
1010
{
1111
path: '/',
12-
component: Home,
1312
name: 'Home',
1413
components: {
1514
default: Home,
1615
'navbar-top': Navbar,
1716
},
1817
},
18+
{
19+
name: 'About',
20+
path: '/about',
21+
components: {
22+
default: About,
23+
'navbar-top': Navbar,
24+
},
25+
},
1926
{
2027
path: '/user',
2128
children: [

0 commit comments

Comments
 (0)