溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue中如何使用vue-router切換頁面時(shí)滾動(dòng)條自動(dòng)滾動(dòng)到頂部

發(fā)布時(shí)間:2021-07-20 10:42:18 來源:億速云 閱讀:278 作者:小新 欄目:web開發(fā)

小編給大家分享一下vue中如何使用vue-router切換頁面時(shí)滾動(dòng)條自動(dòng)滾動(dòng)到頂部,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

有時(shí)候我們需要頁面滾動(dòng)條滾動(dòng)到某一固定的位置,一般使用Window scrollTo() 方法。

語法就是:scrollTo(xpos,ypos)

xpos:必需。要在窗口文檔顯示區(qū)左上角顯示的文檔的 x 坐標(biāo)。

ypos:必需。要在窗口文檔顯示區(qū)左上角顯示的文檔的 y 坐標(biāo)。

例如滾動(dòng)內(nèi)容的坐標(biāo)位置100,500:

window.scrollTo(100,500);

好了,這個(gè)scrollTop這兒只是簡單介紹一下,下面我們介紹下veu-router中的滾動(dòng)行為。

使用前端路由,當(dāng)切換到新路由時(shí),想要頁面滾到頂部,或者是保持原先的滾動(dòng)位置,就像重新加載頁面那樣。 vue-router 能做到,而且更好,它讓你可以自定義路由切換時(shí)頁面如何滾動(dòng)。

注意: 這個(gè)功能只在 HTML5 history 模式下可用。

當(dāng)創(chuàng)建一個(gè) Router 實(shí)例,你可以提供一個(gè) scrollBehavior 方法:

const router = new VueRouter({
 routes: [...],
 scrollBehavior (to, from, savedPosition) {
  // return 期望滾動(dòng)到哪個(gè)的位置
 }
})

scrollBehavior 方法接收 to 和 from 路由對象。第三個(gè)參數(shù) savedPosition 當(dāng)且僅當(dāng) popstate 導(dǎo)航 (通過瀏覽器的 前進(jìn)/后退 按鈕觸發(fā)) 時(shí)才可用。

這個(gè)方法返回滾動(dòng)位置的對象信息,長這樣:

{ x: number, y: number }
{ selector: string, offset? : { x: number, y: number }} (offset 只在 2.6.0+ 支持)

如果返回一個(gè) falsy (譯者注:falsy 不是 false,參考這里)的值,或者是一個(gè)空對象,那么不會發(fā)生滾動(dòng)。

舉例:

scrollBehavior (to, from, savedPosition) {
 return { x: 0, y: 0 }
}

對于所有路由導(dǎo)航,簡單地讓頁面滾動(dòng)到頂部。

返回 savedPosition,在按下 后退/前進(jìn) 按鈕時(shí),就會像瀏覽器的原生表現(xiàn)那樣:

scrollBehavior (to, from, savedPosition) {
 if (savedPosition) {
  return savedPosition
 } else {
  return { x: 0, y: 0 }
 }
}

如果你要模擬『滾動(dòng)到錨點(diǎn)』的行為:

scrollBehavior (to, from, savedPosition) {
 if (to.hash) {
  return {
   selector: to.hash
  }
 }
}

我們還可以利用路由元信息更細(xì)顆粒度地控制滾動(dòng)。

 routes: [
  { path: '/', component: Home, meta: { scrollToTop: true }},
  { path: '/foo', component: Foo },
  { path: '/bar', component: Bar, meta: { scrollToTop: true }}
 ]

完整的例子:

import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const Home = { template: '<div>home</div>' }
const Foo = { template: '<div>foo</div>' }
const Bar = {
 template: `
  <div>
   bar
   <div ></div>
   <p id="anchor">Anchor</p>
  </div>
 `
}
// scrollBehavior:
// - only available in html5 history mode
// - defaults to no scroll behavior
// - return false to prevent scroll
const scrollBehavior = (to, from, savedPosition) => {
 if (savedPosition) {
  // savedPosition is only available for popstate navigations.
  return savedPosition
 } else {
  const position = {}
  // new navigation.
  // scroll to anchor by returning the selector
  if (to.hash) {
   position.selector = to.hash
  }
  // check if any matched route config has meta that requires scrolling to top
  if (to.matched.some(m => m.meta.scrollToTop)) {
   // cords will be used if no selector is provided,
   // or if the selector didn't match any element.
   position.x = 0
   position.y = 0
  }
  // if the returned position is falsy or an empty object,
  // will retain current scroll position.
  return position
 }
}
const router = new VueRouter({
 mode: 'history',
 base: __dirname,
 scrollBehavior,
 routes: [
  { path: '/', component: Home, meta: { scrollToTop: true }},
  { path: '/foo', component: Foo },
  { path: '/bar', component: Bar, meta: { scrollToTop: true }}
 ]
})

new Vue({
 router,
 template: `
  <div id="app">
   <h2>Scroll Behavior</h2>
   <ul>
    <li><router-link to="/">/</router-link></li>
    <li><router-link to="/foo">/foo</router-link></li>
    <li><router-link to="/bar">/bar</router-link></li>
    <li><router-link to="/bar#anchor">/bar#anchor</router-link></li>
   </ul>
   <router-view class="view"></router-view>
  </div>
 `
}).$mount('#app')

在網(wǎng)上查了一下,網(wǎng)友說還可以試試在main.js入口文件配合vue-router寫這個(gè)

router.afterEach((to,from,next) => {
  window.scrollTo(0,0);
});

以上是“vue中如何使用vue-router切換頁面時(shí)滾動(dòng)條自動(dòng)滾動(dòng)到頂部”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI