我正在尝试向只能在 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");
}
}
我已将其绑定(bind)到主体上的 touchend
事件。惯性是触摸结束时旧的 moveX 位置与最新的 moveX 位置之间的差异。
然后,我尝试为包含一堆缩略图的 div 的 scrollLeft
属性设置动画。正如我所说,这在向左滚动时有效,但在向右滚动时无效。
有什么想法吗?
最佳答案
“animate(...)”的第二个参数是持续时间,不能为负数。
尝试:
...
$("#container").animate({
'scrollLeft': $("#container").scrollLeft() + (inertia * 10)
}, Math.abs(inertia * 20));
...
关于jquery - 尝试用 jQuery 实现滚动惯性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2682396/