jQuery - ScrollTop without animation

user970727 picture user970727 · Apr 13, 2012 · Viewed 46k times · Source

How can I use the scrolltop without an animation

This code works:

var offTop = $('#box').offset().top;
offTop  = offTop-43;
$('#mainCt').animate({scrollTop: '+=' + offTop + 'px'}, 400);

And here are my (not working solutions):

$("#mainCt").scrollTop('+=' + offTop + 'px');                 // doesn't work
$("#mainCt").scrollTop('+='+offTop);                          // doesn't work
hhh = setTimeout(' $("#mainCt").scrollTop('+offTop+');',800); // doesn't work

DEMO
http://jsfiddle.net/DNNFF/9/

Answer

Rory McCrossan picture Rory McCrossan · Apr 13, 2012

Try this:

var offTop = $('#box').offset().top - 43;
$('#mainCt').scrollTop(offTop);

The scrollTop property accepts just an integer, no suffixes or units required.