Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll down on page load

I need to scroll down about 50px when the page is loaded. This is what I'm using:

$(window).load(function(){
    $("html,body").scrollTop(55);
});

I've also tried:

scrollTo(0,55)

This works fine in Firefox and IE, however in Chrome, Safari and Opera it scrolls down to the proper position and then jumps back up to the top(or the last scroll position).

I've also tried using an element id to scroll down, but the browser still overwrites it. I tried like this:

htttp://website.com#element
like image 874
Jon Snow Avatar asked Oct 18 '13 13:10

Jon Snow


2 Answers

I think your problem is that you are using $(window).load and some browsers are having problem as things havnt fully rendered yet. Try swapping to

$(document).ready(function(){
    $("html,body").scrollTop(55);
});

Seems to work fine in all browsers here http://jsfiddle.net/7jwRk/1/

Info

$(document).ready

executes when HTML-Document is loaded and DOM is ready

$(window).load

executes when complete page is fully loaded, including all frames, objects and images

like image 116
Dominic Green Avatar answered Oct 06 '22 00:10

Dominic Green


<script type="text/javascript">
   $(document).ready(function(){
     var divLoc = $('#123').offset();
     $('html, body').animate({scrollTop: divLoc.top}, "slow");
 });
 </script>

Add id="123" to any <div> it will automatically scroll it down when page loads.

Here's an another script if the previous one wont work !

  <script>
 window.setInterval(function() {
 var elem = document.getElementById('fixed');
 elem.scrollTop = elem.scrollHeight;  }, 3000);
 </script>

Add id="fixed" to any <div> it will automatically scroll it down when page loads.

like image 35
johnscoop Avatar answered Oct 06 '22 01:10

johnscoop