无论如何使用jQuery在移动设备上顺利制作动画?

时间:2014-09-30 04:38:14

标签: javascript jquery html jquery-animate

加载一个相当长的页面后,我需要平滑地向下滚动到页面上的某个页面,这样用户就不必这样做了。

$(document).ready(function () {
    // Handler for .ready() called.
    $('html, body').animate({
        scrollTop: $('#today').offset().top
    }, 'slow');
});

...
<div id="today">foo</div>

这在桌面浏览器中运行良好,但在iPhone上,特别是在Android上,它非常生涩。

问题:

  1. 我是以正确的方式去做的吗?还有更好的方法吗?
  2. 有没有办法指定特定的时间间隔(以毫秒为单位),而不是“慢”?

1 个答案:

答案 0 :(得分:1)

默认情况下,jQuery .animate() easing选项设置为swing,尝试设置为linear,例如,

$(document).ready(function () {
    // Handler for .ready() called.
    $('html, body').animate({
        scrollTop: $('#today').offset().top
    }, 2000, "linear");
});
#today {
  position : absolute;
  top : 1000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="today">today</div>