2

First, I am really sorry for my poor english...

Now I doing PhoneGap development. I use iScroll to make listview scrolling. And I want to get the scrolling direction.

For sample, when I slide down the screen, the scroller will scroll up.When I slide up the screen, the scroller will scroll down, And how to get the scroller's direction when it scrolling?

can anyone help? I will be grateful!

2 Answers 2

6

I had the same self-question and ended up using .dirX and .dirY properties of the iScroll instance. Their value is always either 1 (left/up), 0 (none) or -1 (right/down), which nicely evaluates to true if the direction is on the particular axis.

new iScroll("your_element_id", {
    onTouchEnd: function(){
        alert(this.dirX); // Returns 1, 0 or -1
        alert(!!this.dirX); // Returns true for horizontal
    }
});

As I need to get the direction of a flick I use onTouchEnd event, but the same applies to onScrollMove (and possibly other events) too.

1
  • 1
    Perfect! Used it with onScrollEnd to perfection! Mar 8, 2013 at 19:30
1

There's no direct way to do this, but you can achieve this through events.

You can use the onScrollStart event to detect when scrolling has started. Record the current position using .x, and start an interval (setInterval) to keep checking the new current position. Then you can do a difference and figure out whether you have scrolled left or right.

Make sure to clear the interval on scrollend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.