0

I'm trying to modify the excellent function I found on here by @timwolla

Basically I want a non JQuery way of smooth scrolling to a div ID

This is what I'm trying but it doesn't scroll at all, the problem is I think, I need to find the position of the ID I'm trying to scroll to:

function scrollTo(element, to, duration) {
  var el = document.getElementById(element);
  if (duration < 0) return;
  var difference = to - el.scrollTop;
  var perTick = difference / duration * 10;

  setTimeout(function() {
    el.scrollTop = el.scrollTop + perTick;
    if (el.scrollTop === to) return;
    scrollTo(el, to, duration - 10);
  }, 10);
}
StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • Can you explain what is the `to` argument ? And is `element` an `Element` or a `string`(id) ? – Sebastien C. Oct 14 '14 at 14:19
  • You mean this solution http://stackoverflow.com/a/8918062/1317927 ? Why did you change it at all? Just pass an element as the first parameter and it should work – Ian Oct 14 '14 at 14:21
  • @Ian Yeah I thought it would too but it doesn't for some reason – StudioTime Oct 14 '14 at 14:24
  • @DarrenSweeney Ahh you're right, I didn't read what that function was meant to do. It's supposed to scroll an element to a specific value. So technically, you'd want to pass the container as the first element, and then pass the `offsetTop` of the target div as the second parameter – Ian Oct 14 '14 at 14:39

0 Answers0