Smooth Scroll to Section
JavaScript
Medium
3 views
Problem Description
You have anchor links with class jump and href like #section1. On click, prevent default and smoothly scroll to the target element.
Output Format
No output (DOM change).
Constraints
Assume links .jump point to existing element ids.
Official Solution
document.querySelectorAll('a.jump').forEach(a=>{a.addEventListener('click',e=>{const href=a.getAttribute('href')||'';if(!href.startsWith('#'))return;e.preventDefault();const target=document.querySelector(href);if(target)target.scrollIntoView({behavior:'smooth',block:'start'});});});
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!