简体   繁体   中英

scroll to top of page not working on mobile

I have looked and tried to implement many solutions from SO, but cannot get it to work. I have tried using the iscroll library, setting timeouts etc.

I want to scroll to the top of the window/page in a mobile phone device when a user clicks a button.

$('.box').click(function(){
   document.body.scrollTop = 0;
});

here you have version which is working on everything with documentation ;)

// ===== Scroll to Top ==== 
$(window).scroll(function()  //When the page is being scrolled
{
    if ($(this).scrollTop() >= 150)    // If page is scrolled more than 150px
    {
        $("#return_to_top").fadeIn(200);    // Fade in the arrow, 200 means that arrow will be shown in 200 miliseconds (fast) - 600 means slow, 400 is normal
    }
    else
    {
        $("#return_to_top").fadeOut(200);   // Else fade out the arrow, fast
    }
});

$(document).ready(function()  //When the page is ready, load function
{
    $("#return_to_top").click(function()  // When arrow is clicked
    {
        $("body,html").animate(
        {
            scrollTop : 0                       // Scroll to top of body
        }, 400);  //how fast the scrolling animation will be in miliseconds
    });
});

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