Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery scrollTop inconsistent across browsers

In Chrome and Safari, $("body").scrollTop(1000) goes where expected.

In IE and FF, nothing happens.

In IE and FF, $(window).scrollTop(1000) works, but they go to different places. It also works in Chrome and Safari, but they both go to a different place as well. They seems to be up to 300-500 pixels off.

Is there any consistent way to set the scrollTop property that works cross browser, and if not, why doesn't jQuery abstract this?

I'd like to animate it as well, which works fine in Chrome and Safari, but not in the other browsers.

Is my only option to do browser detection? (bad practice) Or is there some better way?

like image 877
Nathan Loyer Avatar asked Jan 27 '12 23:01

Nathan Loyer


2 Answers

$(jQuery.browser.webkit ? "body": "html").animate({ scrollTop: myTop }, myDur);

Webkit browsers (Chrome/Safari, both Mac and Win) use "body", others (FF/Opera/IE 7-9) use "html"

Gotta love browser detection.

like image 105
bluescrubbie Avatar answered Nov 14 '22 01:11

bluescrubbie


Try

$(document).scrollTop("...");
like image 6
epignosisx Avatar answered Nov 14 '22 02:11

epignosisx