vue中使用vue-router切換頁面時捲軸自動滾動到頂部的方法,vuevue-router

來源:互聯網
上載者:User

vue中使用vue-router切換頁面時捲軸自動滾動到頂部的方法,vuevue-router

有時候我們需要頁面捲軸滾動到某一固定的位置,一般使用Window scrollTo() 方法。

文法就是:scrollTo(xpos,ypos)

xpos:必需。要在視窗文檔顯示區左上方顯示的文檔的 x 座標。

ypos:必需。要在視窗文檔顯示區左上方顯示的文檔的 y 座標。

例如滾動內容的座標位置100,500:

window.scrollTo(100,500);

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

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

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

當建立一個 Router 執行個體,你可以提供一個 scrollBehavior 方法:

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

scrollBehavior 方法接收 to 和 from 路由對象。第三個參數 savedPosition 若且唯若 popstate 導航 (通過瀏覽器的 前進/後退 按鈕觸發) 時才可用。

這個方法返復原動位置的對象資訊,長這樣:

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

如果返回一個 falsy (譯者註:falsy 不是 false,參考這裡)的值,或者是一個Null 物件,那麼不會發生滾動。

舉例:

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

對於所有路由導航,簡單地讓頁面滾動到頂部。

返回 savedPosition,在按下 後退/前進 按鈕時,就會像瀏覽器的原生表現那樣:

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

如果你要類比『滾動到錨點』的行為:

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

我們還可以利用路由元資訊更細顆粒度地控制滾動。

 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 style="height:500px"></div>   <p id="anchor">Anchor</p>  </div> `}// scrollBehavior:// - only available in html5 history mode// - defaults to no scroll behavior// - return false to prevent scrollconst 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">   <h1>Scroll Behavior</h1>   <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')

在網上查了一下,網友說還可以試試在main.js入口檔案配合vue-router寫這個

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

總結

以上所述是小編給大家介紹的vue中使用vue-router切換頁面時捲軸自動滾動到頂部的方法,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.