What's the best way to implement js scroll to element smoothly? or How can I use js scroll to element

14 Replies, 675 Views

Yo! The default smooth scroll is kinda meh, right? For more control, try GSAP’s ScrollTo plugin. I know you said no libraries, but GSAP is lightweight and gives you insane easing options.

If you’re dead set on vanilla, here’s a quick hack:

```js
const scrollToEl = (element) => {
const targetPos = element.getBoundingClientRect().top + window.scrollY;
window.scrollTo({
top: targetPos,
behavior: 'smooth'
});
};
```

Not perfect, but better than nothing.

Messages In This Thread



Users browsing this thread: 1 Guest(s)