简体   繁体   中英

Scroll page on page load if at top of page

I would like to do something if the page is detected to be at the very top of the page otherwise don't do it. I'm guessing I need to use and if statement somehow but I'm just not sure how to do this.

For example, I want the page to scroll to 125 pixels if the page is at the very top otherwise don't scroll to that position.

$('html, body').animate({scrollTop:125}, 4000);

Use this on load:

function checkTop(){
    if ($(window).scrollTop() == 0) {
        $(window).animate({scrollTop:125}, 4000);
    }
}

checkTop();

$(window).scroll(function(){
    checkTop();
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM