Kelvin Zhao
Kelvin Zhao

Reputation: 2425

Scroll direction on phone?

How do I detect scroll direction on phones? I am currently using

$window.on( 'touchmove', onPhone );

to call an onPhone function when touch scrolling happens. But how do I detect if it's scrolling down or up?

Thanks in advance!

Upvotes: 1

Views: 2243

Answers (1)

Mark183
Mark183

Reputation: 126

Im assuming you only want scroll up and down direction.

var lastPoint = null; //global

$(window).on('touchend', function(e){

    var currentPoint = e.originalEvent.changedTouches[0].pageY;

    if(lastPoint != null && lastPoint < currentPoint ){
        //swiped down
        console.log('you scrolled up');

    }else if(lastPoint != null && lastPoint > currentPoint){
        //swiped up
        console.log('you scrolled down');
    }

    lastPoint = currentPoint;
});

If you want to detect left right scroll directions can use the same code but change 'pageY' to 'pageX'.

Hope this helps!

Upvotes: 3

Related Questions