Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to when page load

i got a jquery code from this link (end of page): How to scroll to top of page with JavaScript/jQuery?

Why don't you just use some reference element at the very beginning of your html file, like

<div id="top"></div>

and then, when the page loads, simply do

$(document).ready(function(){

    top.location.href = '#top';

});

If the browser scrolls after this function fires, you simply do

$(window).load(function(){

    top.location.href = '#top';

});

now, everything is working but not in google chrome! how i can fix this code for google chrome? and how i can add a some animation to this code? like scroll speed or fast, slow etc...

like image 303
Alireza Avatar asked Jun 20 '12 03:06

Alireza


People also ask

How do I scroll down on page load?

also Hot key Ctrl +End will take you directly to the bottom of the page.

How do you load the webpage content based on user scrolling?

You can use window. addeventlistener to track the scrolling behavior the webpage and load more content to the page when the user is at the foot of the webpage.

How do I load a div to scroll?

Just get the div id from query string and on page load pass it to above code like: $('#' + divToScrollTo). offset(). top , where divToScrollTo is js variable where we have stored the query string value.


1 Answers

If you're using jQuery, you can use scrollTop to scroll. It's a method, but it can be animated too. Here is the documentation: jQuery API Documentation for scrollTop

You can use it like so:

$("html,body").scrollTop(0);

Or for animating:

$("html,body").animate({scrollTop: 0}, 1000);

You could set this in any event handler:

$(document).ready(function()
{
     $("html,body").animate({scrollTop: 0}, 1000);
}

Or:

$(window).load(function()
{
     $("html,body").animate({scrollTop: 0}, 1000);
}
like image 147
lewiguez Avatar answered Sep 20 '22 12:09

lewiguez