25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Scroll behavior<br />

Another aspect of website navigation that the browser automatically manages is<br />

scroll behavior. For example, if you scroll to the bottom of a page, then navigate<br />

to a new page, the scroll position is reset. But if you return to the previous page,<br />

the scroll position is remembered by the browser, <strong>and</strong> you're taken back to the<br />

bottom.<br />

The browser can't do this when we've hijacked navigation with <strong>Vue</strong> Router. So,<br />

when you scroll to the bottom of the <strong>Vue</strong>bnb home page <strong>and</strong> click a listing in<br />

Cuba, let's say, the scroll position is unchanged when the listing page component<br />

is loaded. This feels really unnatural to the user, who would expect to be taken to<br />

the top of the new page:<br />

Figure 7.17. Scroll position issue after navigating with <strong>Vue</strong> Router <strong>Vue</strong> Router has a scrollbehavior<br />

method that allows you to adjust where the page is scrolled when you change routes by simply defining the<br />

x <strong>and</strong> y positions of the horizontal <strong>and</strong> vertical scroll bars. To keep it simple, <strong>and</strong> yet to still keep the UX<br />

natural, let's make it so we are always at the top of the page when a new page is loaded.<br />

resources/assets/<strong>js</strong>/router.<strong>js</strong>: export default new <strong>Vue</strong>Router({ mode: 'history',<br />

routes: [ ... ], scrollBehavior (to, from, savedPosition) { return { x: 0, y: 0 } } });

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!