Hi jemz,
I was actually surprised at how difficult this was to achieve in a cross-browser compatible way.
The secret was to wrap the call to scrollTop() in a setTimeout(), so that the browser has time to position itself before you reset the scroll position.
Here's the code:
HTML Code:
<!DOCTYPE HTML>
<html>
<head>
<!-- [url]http://www.sitepoint.com/forums/showthread.php?977666-window-problem[/url] -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Set scrollTop</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<style>#main{ background:green; height:3000px; width:500px; }</style>
</head>
<body>
<div id="main">
<h1>Test Div</h1>
</div>
<p>Test Paragraph</p>
<script>
$(document).ready(function() {
setTimeout(function() { $("html, body").scrollTop(0); }, 150);
});
</script>
</body>
</html>
Bookmarks