Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - ScrollTop without animation

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/

like image 287
user970727 Avatar asked Apr 13 '12 15:04

user970727


3 Answers

Try this:

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

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

like image 160
Rory McCrossan Avatar answered Oct 18 '22 05:10

Rory McCrossan


maybe if you don't want an animation or anything fancy just use an anchor

<a name="top"></a>

Place it where you need to scroll

and in your function where you are calling use

document.location.href="#top";

You could also create a function to append the anchor before the element, do the document.location thing and later remove that anchor.

http://jsfiddle.net/fSrxr/1/

like image 18
chepe263 Avatar answered Oct 18 '22 03:10

chepe263


Skip jQuery. Just use JavaScript:

window.scroll(0, 0);
like image 18
Jeroen Flamman Avatar answered Oct 18 '22 04:10

Jeroen Flamman