代码之家  ›  专栏  ›  技术社区  ›  gargantuan

尝试用jquery实现滚动惯量

  •  2
  • gargantuan  · 技术社区  · 14 年前

    我试图在一个只在iPad上浏览的网页上增加一些iPhone风格的滚动惯性。

    我让滚动在一个方向上工作(向左滚动),但在另一个方向上不工作。这是一个非常简单的函数

    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”事件绑定在一起。Intertia是触摸结束时旧的movex位置和最新的movex位置之间的差异。

    然后我尝试对包含大量缩略图的DIV的ScrollLeft属性进行动画处理。正如我所说,这在向左滚动时有效,但在向右滚动时无效。

    有什么想法吗?

    1 回复  |  直到 10 年前
        1
  •  4
  •   dix    14 年前

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

    尝试:

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