98

In a react app, I have a method being called to bring a particular node into view as follows.

scrollToQuestionNode(id) {
        const element = document.getElementById(id);
        element.scrollIntoView(false);
}

The scroll happens fine, but the scroll action is a little jerky. How can I make it smooth? I don't see any options which I can give to scrollIntoView for the same.

1
  • It's now supported by Chrome (from Chrome 61) Commented Oct 20, 2017 at 18:35

5 Answers 5

205

This might help.

From MDN documentation of scrollIntoView You can pass in option instead of boolean.

scrollIntoViewOptions Optional
A Boolean or an object with the following options:
{
  behavior: "auto"  | "instant" | "smooth",
  block:    "start" | "center" | "end" | "nearest",
  inline:    "start" | "center" | "end" | "nearest",
}

So you can simply pass parameter like this.

scrollToQuestionNode(id) {
  const element = document.getElementById(id);
  element.scrollIntoView({ block: 'end',  behavior: 'smooth' });
}

Reference: https://developer.mozilla.org/en/docs/Web/API/Element/scrollIntoView

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks lavish, but I already tried it. As you can see in the link you mentioned, scrollIntoViewOptions is not supported in chrome ( I find it strange, but thats the reality)
It's now supported by Chrome (from Chrome 61)
it won't work in safari and IE browsers as per MDN documentation
That is correct, IE, edge etc does not support smooth behaviour unfortunately.
There is a polyfill for non supporting browsers: link
|
26

For multibrowser support use the smooth-scroll-polyfill from here

For easy implementation use a wrapper like this around the polyfill so the .js polyfill method would be inizialized after loading:

https://codepen.io/diyifang/embed/MmQyoQ?height=265&theme-id=0&default-tab=js,result&embed-version=2

Now this should work cross browser:

document.querySelector('.foo').scrollIntoView({
  behavior: 'smooth'
});

1 Comment

It should be noted that for Firefox, the user has to change the general.smoothScroll configuration. guidingtech.com/how-to-enable-firefox-smooth-scrolling Otherwise, it works out of the box.
11

Use this CSS on the div with the scrollbar:

.element-with-the-scrollbar {
  overflow-y: scroll;
  scroll-behavior: smooth;
}

This gives smooth scrolling even when you just do:

elementWithTheScrollbar.scrollTop = 0;

1 Comment

This is the only right solution, because scrollIntoView does not support both false and a scrollIntoViewOptions as args: only one of these can be used at a time.
2

What saved me was applying CSS smooth behavior with JS:

document.documentElement.style.scrollBehavior = "smooth";

Just before calling the function

document.getElementById("id-name").scrollIntoView();

Comments

0

I don't know if this can help too but I'm working on a Vue app I had a similar need, since removing one of the last elements on a page list left blank space on the bottom (I needed to remove the blank space by scrolling down). So, the removing happens through a button function. After all's done and when it's needed, I'm targeting the last div in the list and stating

div.scrollIntoView({block: "end", behavior: "smooth"})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.