Home> Web Front-end> Vue.js> body text

How to use Vue Router to implement page scrolling behavior control?

WBOY
Release: 2023-07-21 18:49:07
Original
1316 people have browsed it

How to use Vue Router to implement page scrolling behavior control?

When using Vue Router for single-page application development, we often need to control the page scrolling behavior to achieve a smoother user experience. Vue Router provides a simple and flexible way to control page scrolling behavior. This article will introduce how to use Vue Router to control page scrolling behavior, and attach code examples for reference.

Vue Router's scrolling behavior control is implemented through thescrollBehaviorfunction. When creating a Vue Router instance, we can pass a customscrollBehaviorfunction as a parameter. This function receives three parameters:torepresents the route that is about to enter,fromrepresents the route that is about to leave,savedPositionrepresents the last scrolling position.

The following is a simple sample code:

import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) const routes = [ // 路由配置 ] const router = new VueRouter({ routes, scrollBehavior (to, from, savedPosition) { // 控制滚动行为的逻辑 } })
Copy after login

Next, we can write our own control logic in thescrollBehaviorfunction according to actual needs. The following are several common scrolling behavior control methods:

  1. Scroll to the specified anchor point position
scrollBehavior (to, from, savedPosition) { if (to.hash) { return { selector: to.hash } } }
Copy after login

When the route jumps, if the target route has an anchor point (i.e. If the URL contains an identifier followed by#), the page will scroll to the location of the corresponding anchor.

  1. Scroll to the last location
scrollBehavior (to, from, savedPosition) { if (savedPosition) { return savedPosition } }
Copy after login

If the user jumps from one route to another, and then returns to the original route, the page will scroll to the top The location left.

  1. Scroll to the top of the page
scrollBehavior (to, from, savedPosition) { return { x: 0, y: 0 } }
Copy after login

The page will scroll to the top position every time the route jumps.

  1. Customized scrolling behavior logic
scrollBehavior (to, from, savedPosition) { // 编写自己的滚动行为逻辑 }
Copy after login

The above are just some common scrolling behavior control methods. In actual applications, more complex logic may need to be written according to specific needs.

In addition to controlling scrolling behavior in thescrollBehaviorfunction, we can also control scrolling behavior through global navigation guards. For example, in thebeforeEachnavigation guard, we can control the scrolling behavior through thewindow.scrollTomethod.

router.beforeEach((to, from, next) => { // 控制滚动行为的逻辑 window.scrollTo(0, 0) next() })
Copy after login

Through the above methods, we can easily control the page scrolling behavior in Vue Router. Choose the corresponding control method according to actual needs, and customize it appropriately according to your own project. Hope this article helps you!

The above is the detailed content of How to use Vue Router to implement page scrolling behavior control?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!