尝试用 jQuery 实现滚动惯性

发布于 2024-08-30 05:03:07 字数 975 浏览 2 评论 0原文

我正在尝试向只能在 iPad 上查看的网页添加一些 iPhone 风格的滚动惯性。

我可以在一个方向上滚动(scrollLeft),但在另一方向上则不起作用。这是一个非常简单的函数:

function onTouchEnd(event){
                event.preventDefault();
                inertia = (oldMoveX - touchMoveX);
                
                // Inertia Stuff
                if( Math.abs(inertia) > 10 ){

                    $("#feedback").html(inertia);
                    
                    $("#container").animate({
                        'scrollLeft': $("#container").scrollLeft() + (inertia * 10)
                    }, inertia * 20);
                    
                }else{
                    
                    $("#feedback").html("No Inertia");
                    
                }
                
            }

我将其绑定到主体上的 touchend 事件。惯性是触摸结束时旧的 moveX 位置与最新的 moveX 位置之间的差值。

然后,我尝试为包含一堆缩略图的 div 的 scrollLeft 属性设置动画。正如我所说,这在向左滚动时有效,但在向右滚动时无效。

有什么想法吗?

I'm trying to add some iPhone style scrolling inertia to a web page that will only be viewed on the iPad.

I have the scrolling working in one direction (scrollLeft), but it doesn't work in the other direction. It's a pretty simple function:

function onTouchEnd(event){
                event.preventDefault();
                inertia = (oldMoveX - touchMoveX);
                
                // Inertia Stuff
                if( Math.abs(inertia) > 10 ){

                    $("#feedback").html(inertia);
                    
                    $("#container").animate({
                        'scrollLeft': $("#container").scrollLeft() + (inertia * 10)
                    }, inertia * 20);
                    
                }else{
                    
                    $("#feedback").html("No Inertia");
                    
                }
                
            }

I've bound it to the touchend event on the body. The inertia is the difference between he old moveX position and the latest moveX position when a touch ends.

I then try to animate the scrollLeft property of a div that contains a bunch of thumbnails. As I've said, this works when scrolling to the left, but not when scrolling to the right.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入画浅相思 2024-09-06 05:03:07

“animate(...)”的第二个参数是持续时间,它不能为负数。

尝试:

...
$("#container").animate({
      'scrollLeft': $("#container").scrollLeft() + (inertia * 10)
   }, Math.abs(inertia * 20));
...

the 2nd argument to "animate(...)" is the duration, which must not be negative.

try:

...
$("#container").animate({
      'scrollLeft': $("#container").scrollLeft() + (inertia * 10)
   }, Math.abs(inertia * 20));
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文