简体   繁体   中英

NextJS router push with scroll to a component on another page

suppose I'm on Page1 and a button that makes a router.push to Page2, but I need that when Page2 loads, it automatically scrolls to a component that is on Page2, but I don't know how it could be done or even if it is possible. Does anyone have any ideas? Thank you

You can redirect with a hash in the url:

router.push('mypage#myel');

This will then scroll to an element with the ID of myel .

<div id='myel'></div>

You can find more here: https://blog.httpwatch.com/2011/03/01/6-things-you-should-know-about-fragment-urls/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM