【问题标题】:Scroll to top only on mobile仅在移动设备上滚动到顶部
【发布时间】:2015-09-18 08:26:11
【问题描述】:

我想到达页面顶部,但只能在移动设备上,而不是在窗口/计算机上。

这是我使用的代码:

$(document).ready(function () {

$(window).scroll(function () {
    if ($(this).scrollTop() > 100) {
        $('.scrollup').fadeIn();
    } else {
        $('.scrollup').fadeOut();
    }
});

$('.scrollup').click(function () {
    $("html, body").animate({
        scrollTop: 0
    }, 600);
    return false;
});

现在有人怎么做吗?

【问题讨论】:

    标签: javascript mobile scroll


    【解决方案1】:

    是的。

    此代码应能检测到大多数移动浏览器。

    $('.scrollup').click(function () {
        if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
            $("html, body").animate({
                scrollTop: 0
            }, 600);
            return false;
        }
    });
    

    查看此答案了解更多信息:What is the best way to detect a mobile device in jQuery?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-02
      • 2014-06-20
      • 2016-05-06
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 2020-04-18
      相关资源
      最近更新 更多