Complete the code to enable smooth scrolling when clicking the link.
export default function Home() {
return (
<a href="#section" style={{ scrollBehavior: '[1]' }}>Go to Section</a>
)
}The scrollBehavior CSS property controls the scrolling animation. Using "smooth" makes the scroll transition smooth.
Complete the code to scroll to the top smoothly when the button is clicked.
import { useRouter } from 'next/router'; export default function ScrollTop() { const router = useRouter(); function handleClick() { window.scrollTo({ top: 0, behavior: '[1]' }); } return <button onClick={handleClick}>Scroll to Top</button>; }
The behavior option in window.scrollTo controls the scroll animation. Setting it to "smooth" scrolls smoothly to the top.
Fix the error in the code to prevent the page from jumping instantly when navigating.
import { useRouter } from 'next/router'; import { useEffect } from 'react'; export default function Page() { const router = useRouter(); useEffect(() => { router.events.on('routeChangeComplete', () => { window.scrollTo({ top: 0, behavior: '[1]' }); }); }, [router.events]); return <div>Content</div>; }
Using behavior: 'smooth' in window.scrollTo ensures the page scrolls smoothly after route changes instead of jumping instantly.
Fill both blanks to create a scrollable container with smooth scroll behavior and vertical scrolling.
<div style={{ overflowY: '[1]', scrollBehavior: '[2]', height: '200px' }}>
<p>Lots of content here...</p>
</div>overflowY: 'scroll' enables vertical scrolling. scrollBehavior: 'smooth' makes scrolling smooth inside the container.
Fill all three blanks to create a Next.js link that scrolls smoothly to the target section with offset top 100.
import Link from 'next/link'; export default function Nav() { function handleClick(e) { e.preventDefault(); const target = document.getElementById('[1]'); if (target) { window.scrollTo({ top: target.offsetTop [2] 100, behavior: '[3]' }); } } return <Link href="#[1]" onClick={handleClick}>Go to Section</Link>; }
The id to scroll to is 'section'. The offset is subtracted by 100 pixels using -. The scroll behavior is set to 'smooth' for smooth animation.