如何使用jQuery在移动网络浏览器上设置滚动动画

How do I animate scroll on a mobile webbrowser using jQuery?

本文关键字:浏览器 设置 滚动 动画 网络 移动网 何使用 jQuery 移动      更新时间:2023-09-26

我不想实现的是当按下按钮时页面滚动到某个位置。我在Chrome和IE等浏览器上都有这个功能,但它似乎在任何移动浏览器上都不起作用。

这是我使用的代码:

$("#programmaMenu").on("click", "div", function(event){
        event.preventDefault();
        $('html').stop().animate({
            scrollTop: $("#"+$(this).attr("rel")).position().top - 120
        }, 500, 'swing');
});

事件确实启动,只是滚动没有发生。

感谢用户vohuman,这就是答案:

显然,移动浏览器滚动body元素,桌面浏览器滚动html元素。因此,问题的解决方案是选择元素"html"answers"body",如下所示:

$("#programmaMenu").on("click", "div", function(event){
        event.preventDefault();
        $('html, body').stop().animate({
            scrollTop: $("#"+$(this).attr("rel")).position().top - 120
        }, 500, 'swing');
});

使用这个并尝试,

    $(document).on("click", "#programmaMenu", function(event){
    event.preventDefault();
    $('html').stop().animate({
        scrollTop: $("#"+$(this).attr("rel")).position().top - 120
    }, 500, 'swing');
    });