[b]"What's the best way to implement js scroll to element smoothly?"[/b] or [b]"How can I use js scroll to element

14 Replies, 457 Views

"Best way to implement js scroll to element smoothly?"

Hey folks!

I’ve been trying to get a smooth js scroll to element working on my site, but it either jumps or feels clunky.

I’ve tried `window.scrollTo()` with behavior: 'smooth', but it’s not consistent across browsers.

Any tips for a modern, reliable approach?

Also, is there a way to add custom easing or animations? Or am I stuck with the default smooth scroll?

Thanks in advance!

---

*P.S. If you’ve got a vanilla JS solution, even better. Trying to avoid libraries if possible.*
Hey! For a smooth js scroll to element, I’ve had good luck with `element.scrollIntoView({ behavior: 'smooth' })`. It’s vanilla JS and works decently across browsers.

If you want custom easing, you might need to ditch the native smooth scroll and use `requestAnimationFrame` with a custom easing function. Check out this tutorial on CSS-Tricks for a deep dive: https://css-tricks.com/snippets/jquery/s...scrolling/ (yes, it’s jQuery, but the logic translates to vanilla).

Also, Safari can be a pain—polyfills like `smoothscroll-polyfill` might help if you’re hitting inconsistencies.
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.
Honestly, the native js scroll to element is hit or miss. I’ve found that adding a tiny delay with `setTimeout` can help with the clunkiness.

```js
setTimeout(() => {
document.getElementById('yourElement').scrollIntoView({ behavior: 'smooth' });
}, 100);
```

Weird, but it works. For easing, you’d need to manually animate the scroll position using `window.scrollY` and a lerp function. Mozilla’s docs have a solid example.
If you’re dealing with inconsistent behavior, try checking if the browser supports smooth scroll first:

```js
const supportsSmoothScroll = 'scrollBehavior' in document.documentElement.style;
```

If not, fall back to a polyfill or a custom solution. For easing, you could use a bezier curve with `requestAnimationFrame`—there’s a cool demo on CodePen (just search "smooth scroll easing vanilla js").
Wow, thanks for all the suggestions! I tried the `scrollIntoView` with the polyfill, and it’s way better. Still a bit choppy on older Safari, but the custom `requestAnimationFrame` approach looks promising.

Quick Q: For the manual animation, how do you handle interruptions (like user scrolling mid-animation)? Should I add a cancel flag or something?

Also, GSAP seems cool—might give it a shot if the vanilla route gets too messy. Appreciate the help!
Dude, I feel you. The default js scroll to element is janky af in some browsers.

Try this vanilla approach:

```js
function smoothScrollTo(target) {
const start = window.scrollY;
const distance = target.getBoundingClientRect().top;
const duration = 1000;
let startTime = null;

function animation(currentTime) {
if (!startTime) startTime = currentTime;
const timeElapsed = currentTime - startTime;
const ease = Math.min(timeElapsed / duration, 1);
window.scrollTo(0, start + distance * ease);
if (timeElapsed < duration) requestAnimationFrame(animation);
}

requestAnimationFrame(animation);
}
```

Mess with the `duration` and easing function to tweak it.
For a super simple js scroll to element, this works pretty well:

```js
document.querySelector('#yourElement').scrollIntoView({
behavior: 'smooth',
block: 'start'
});
```

But if you want fancy easing, you’ll need to roll your own or use a lib. Check out https://greensock.com/scrolltoplugin/ for GSAP’s solution—it’s worth the small footprint.



Users browsing this thread: 1 Guest(s)