1

Suppose I wanted to scroll to an object, but I wanted it to go, say, 50 pixels further. I know the following would work:

document.getElementById("foo").scrollIntoView();
document.documentElement.scrollTop += 50;

But that looks a bit bodgy. It's hard to read, and I suspect it will run slightly slower, as it's scrolling twice instead of once.

Is there a one-liner that does the same thing? Something like

document.documentElement.scrollTop = document.getElementById("foo").fooDistance + 50;

I know fooDistance is wrong. What's the correct version?

I tried looking on this list but couldn't see anything that looked right.

1 Answers1

0

maybe Window scrollTo() with element.getBoundingClientRect()?

var posY = element.getBoundingClientRect().top;
window.scrollTo(0, posY+50); 
123
  • 595
  • 6
  • 18