Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jQuery在移动Web浏览器上设置滚动动画?_Javascript_Jquery_Html_Mobile_Mobile Browser - Fatal编程技术网

Javascript 如何使用jQuery在移动Web浏览器上设置滚动动画?

Javascript 如何使用jQuery在移动Web浏览器上设置滚动动画?,javascript,jquery,html,mobile,mobile-browser,Javascript,Jquery,Html,Mobile,Mobile Browser,我不想实现的是当按下按钮时页面滚动到某个位置。我在诸如Chrome和IE之类的浏览器上使用过这个功能,但在任何移动浏览器上都不起作用 这是我使用的代码: $("#programmaMenu").on("click", "div", function(event){ event.preventDefault(); $('html').stop().animate({ scrollTop: $("#"+$(this).attr("rel")).

我不想实现的是当按下按钮时页面滚动到某个位置。我在诸如Chrome和IE之类的浏览器上使用过这个功能,但在任何移动浏览器上都不起作用

这是我使用的代码:

$("#programmaMenu").on("click", "div", function(event){
        event.preventDefault();
        $('html').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');
    });
感谢用户,以下是答案:

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

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

尝试同时选择
html
body
元素:
$('html,body')
谢谢,这就是答案。事件确实会触发,但不会发生滚动。我会更新问题的,好的。尝试在jquery的选择器中添加html、正文$('html,body').stop().animate({});或者,您可以尝试从函数中删除swing参数。
.stop()
此处的用法是停止上一个动画。