Scroll down page load

I need to scroll down about 50 pixels when loading a page. This is what I use:

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

I also tried:

 scrollTo(0,55) 

This works fine in Firefox and IE, however in Chrome, Safari and Opera, it scrolls to the desired position and then returns up (or to the last scroll position).

I also tried using the item id to scroll down, but the browser is still overwriting it. I tried like this:

 htttp://website.com#element 
+6
source share
5 answers

I think your problem is that you are using $ (window) .load, and some browsers have problems, since all havnt is fully displayed. Try swapping on

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

It seems to work well in all browsers here http://jsfiddle.net/7jwRk/1/

Info

$ (document) .ready

runs when loading an HTML document and DOM is ready

$ (window) .load

runs when a full page is fully loaded, including all frames, objects, and images

+4
source

You can use the scrollIntoView () function. This is supported by most browsers (even IE6).

 document.getElementById('header').scrollIntoView() 
+2
source
 <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 down when the page loads.

Here's another script if the previous one doesn't 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 down when the page loads.

+1
source

Alternatively, just run this at the end of your body:

 ... <script type="text/javascript"> $("html,body").scrollTop(55); </script> </body> </html> 
0
source

If you use 2 monitors, make sure that when you open your browser window with the page for which the script is implemented, you do not move this window to another monitor with a different screen resolution. In short: do not switch browser windows to another monitor, just open a new browser window for each monitor / screen resolution.

0
source

Source: https://habr.com/ru/post/956242/


All Articles