Trying to implement scroll inertia with jQuery

I am trying to add some iPhone style scrolling inertia to a webpage that will only display on iPad.

I have a scroll 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 tied it to a "touch" event on the body. The interline is the difference between its old moveX position and the last moveX position when contact ends.

Then I am trying to animate the scrollLeft property for a div that contains a bunch of thumbnails. As I said, this works when scrolling to the left, but not when scrolling to the right.

Any ideas?

+2


a source to share


1 answer


the second argument to animate (...) is the duration, which must not be negative.

try:



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

      

+4


a source







All Articles