0

Hello my current JavaScript is this, to scroll the page to an id tag further down my website page:

function scroll(element){   
    var ele = document.getElementById(element);  
    window.scrollTo(ele.offsetLeft,ele.offsetTop);
    window.scrollBy(0, -100);
}

How can I make the function scroll smoothly ?

The Head Rush
  • 3,157
  • 2
  • 25
  • 45
Joe Wild
  • 1
  • 1
  • see this [link](http://stackoverflow.com/questions/17722497/scroll-smoothly-to-specific-element-on-page) I think it can help you :) – Mouaici_Med Mar 03 '17 at 16:29
  • Possible duplicate of [Scroll smoothly to specific element on page](http://stackoverflow.com/questions/17722497/scroll-smoothly-to-specific-element-on-page) – Heretic Monkey Mar 03 '17 at 18:15

1 Answers1

0

First of all, this is not Java, this is JavaScript, and those two are different, they are not just two names for one programming language... For your problem, I would do something like getting the current position of the scroll by using document.documentElement.scrollTop (but this one returns 0 on chrome) and the wanted position of the scroll by getting your element's coordinates, and then just move the scroll for a bit of the distance every frame by using window.scrollBy() method. However, I would not suggest this method, because there are other, better solution would be to use this

  • @HassanZia Click the "edited x time ago" link under the question to see the original version, where he uses Java in the title. – Heretic Monkey Mar 03 '17 at 18:14