Rectangle 27 0

javascript How to scroll the window using JQuery $.scrollTo() function?


$("#id").scrollTop($("#id").scrollTop() + 100);

If it's not working why don't you try using jQuery's scrollTop method?

If you're looking to scroll smoothly you could use basic javascript setTimeout/setInterval function to make it scroll in increments of 1px over a set length of time.

Note if you want to scroll the whole page and not individual element, use $('html, body') just like Tim has pointed here. Just $('body') will not work in all browsers.

ScrollTop works much more smoothly in IE as well.

Note
Rectangle 27 0

javascript How to scroll the window using JQuery $.scrollTo() function?


$.scrollTo('+=100px', 800, { axis:'y' });

Looks like you've got the syntax slightly wrong... I'm assuming based on your code that you're trying to scroll down 100px in 800ms, if so then this works (using scrollTo 1.4.1):

Note
Rectangle 27 0

javascript How to scroll the window using JQuery $.scrollTo() function?


$('html, body').animate({scrollTop: $("#page").offset().top}, 2000);

+1 for this, very smooth.

+1 worked for me ;) I also am interested to know why html, body instead of just html?

@ScottGreenfield, @Kato: not sure why, but this comment says not including body breaks this on Chrome 4: stackoverflow.com/questions/1890995/

I've often wondered why people use 'html, body' for scrollTop instead of just 'html'. Any thoughts on this?

Note
Rectangle 27 0

javascript How to scroll the window using JQuery $.scrollTo() function?


$(window).scrollTop()
$(window).scrollTop(val)
$({myScrollTop:window.pageYOffset}).animate({myScrollTop:300}, {
  duration: 600,
  easing: 'swing',
  step: function(val) {
    window.scrollTo(0, val);
  }
});
window.pageYOffset
window.scrollTo(0, val)

I never thought to pass an object to jQuery's animate method like that. So many possible uses. This solution is great, thank you.

This works nicely without any refresh gotchas as it's using cross-browser JavaScript.

To get around the html vs body issue, I fixed this by not animating the css directly but rather calling window.scrollTo(); on each step:

Note
Rectangle 27 0

javascript How to scroll the window using JQuery $.scrollTo() function?


$("#id").animate({"scrollTop": $("#id").scrollTop() + 100});
$("#id").offset().top

jQuery now supports scrollTop as an animation variable.

Note
Rectangle 27 0

javascript How to scroll the window using JQuery $.scrollTo() function?


function scrollTo(prop){
    $('html,body').animate({scrollTop: $("#"+prop).offset().top 
+ parseInt($("#"+prop).css('padding-top'),10) 
+ parseInt($("#"+prop).css('margin-top'),10) +},'slow');
}

will work nicely and support padding. You can also support margins easily - for completion see below

Note
Rectangle 27 0

javascript How to scroll the window using JQuery $.scrollTo() function?


$("#id").scrollTop($("#id").scrollTop() + 100);

If it's not working why don't you try using jQuery's scrollTop method?

If you're looking to scroll smoothly you could use basic javascript setTimeout/setInterval function to make it scroll in increments of 1px over a set length of time.

Note if you want to scroll the whole page and not individual element, use $('html, body') just like Tim has pointed here. Just $('body') will not work in all browsers.

ScrollTop works much more smoothly in IE as well.

Note
Rectangle 27 0

javascript How to scroll the window using JQuery $.scrollTo() function?


$(window).scrollTop()
$(window).scrollTop(val)
$({myScrollTop:window.pageYOffset}).animate({myScrollTop:300}, {
  duration: 600,
  easing: 'swing',
  step: function(val) {
    window.scrollTo(0, val);
  }
});
window.pageYOffset
window.scrollTo(0, val)

I never thought to pass an object to jQuery's animate method like that. So many possible uses. This solution is great, thank you.

This works nicely without any refresh gotchas as it's using cross-browser JavaScript.

To get around the html vs body issue, I fixed this by not animating the css directly but rather calling window.scrollTo(); on each step:

Note
Rectangle 27 0

javascript How to scroll the window using JQuery $.scrollTo() function?


$('html, body').animate({scrollTop: $("#page").offset().top}, 2000);

+1 for this, very smooth.

+1 worked for me ;) I also am interested to know why html, body instead of just html?

@ScottGreenfield, @Kato: not sure why, but this comment says not including body breaks this on Chrome 4: stackoverflow.com/questions/1890995/

I've often wondered why people use 'html, body' for scrollTop instead of just 'html'. Any thoughts on this?

Note
Rectangle 27 0

javascript How to scroll the window using JQuery $.scrollTo() function?


$.scrollTo('+=100px', 800, { axis:'y' });

Looks like you've got the syntax slightly wrong... I'm assuming based on your code that you're trying to scroll down 100px in 800ms, if so then this works (using scrollTo 1.4.1):

Note