有時(shí)候我們需要頁(yè)面滾動(dòng)條滾動(dòng)到某一固定的位置,一般使用Window scrollTo()
方法。
語(yǔ)法就是: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這兒只是簡(jiǎn)單介紹一下,下面我們介紹下veu-router中的滾動(dòng)行為。
使用前端路由,當(dāng)切換到新路由時(shí),想要頁(yè)面滾到頂部,或者是保持原先的滾動(dòng)位置,就像重新加載頁(yè)面那樣。 vue-router 能做到,而且更好,它讓你可以自定義路由切換時(shí)頁(yè)面如何滾動(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 路由對(duì)象。第三個(gè)參數(shù) savedPosition 當(dāng)且僅當(dāng) popstate 導(dǎo)航 (通過(guò)瀏覽器的 前進(jìn)/后退 按鈕觸發(fā)) 時(shí)才可用。
這個(gè)方法返回滾動(dòng)位置的對(duì)象信息,長(zhǎng)這樣:
{ x: number, y: number } { selector: string, offset? : { x: number, y: number }} (offset 只在 2.6.0+ 支持)
如果返回一個(gè) falsy (譯者注:falsy 不是 false,參考這里)的值,或者是一個(gè)空對(duì)象,那么不會(huì)發(fā)生滾動(dòng)。
舉例:
scrollBehavior (to, from, savedPosition) { return { x: 0, y: 0 } }
對(duì)于所有路由導(dǎo)航,簡(jiǎn)單地讓頁(yè)面滾動(dòng)到頂部。
返回 savedPosition,在按下 后退/前進(jìn) 按鈕時(shí),就會(huì)像瀏覽器的原生表現(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 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 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"> <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')
在網(wǎng)上查了一下,網(wǎng)友說(shuō)還可以試試在main.js入口文件配合vue-router寫(xiě)這個(gè)
router.afterEach((to,from,next) => { window.scrollTo(0,0); });
總結(jié)
以上所述是小編給大家介紹的vue中使用vue-router切換頁(yè)面時(shí)滾動(dòng)條自動(dòng)滾動(dòng)到頂部的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com