Dicksam Fung
Dicksam Fung

Reputation: 31

How to get the scrolling direction in iScroll

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!

Upvotes: 2

Views: 3752

Answers (2)

Petr Vostrel
Petr Vostrel

Reputation: 2332

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.

Upvotes: 6

Ken Woo
Ken Woo

Reputation: 118

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.

Upvotes: 1

Related Questions