Touchend not firing after touchmove

拥有回忆 提交于 2020-01-01 04:43:05

问题


I'm trying to make a page for mobile devices that detects the scrollTop position and scrolls to the top of the page if scrollTop is lower than half the document height or scroll to bottom if its not.

I have achieved that by using this:

var ScrollTimeout;
$(window).on('scroll',function(){
    clearTimeout(ScrollTimeout);
    ScrollTimeout = setTimeout(scrollToTopOrBottom,200);
    });

The problem is that the timeout fires when the user has stopped scrolling but still has the finger on the screen.

Then I worked with the touchend event and it was great.

$(document).on('touchend',function(){
    scrollToTop();
    });

The user could stopped scrolling (with the finger still on the screen) and then continue scrolling without triggering the scrollToTopOrBottom() function.

The problem is, that event is incosistent between browsers:

In some browsers (Maxthon and Android), the touchend event worked as intended but in Opera Mobile and Chrome, the touchend event doesn't fires. The explanation for this is that touchend doesn't fires because touchcancel has been fired before.

I've tried this

$(document).on('touchmove',function(e){
    e.preventDefault();
    });

and succesfully avoided the triggering of touchcancel, but unluckily also avoided the natural behaviour of scrolling.

Does anyone know how can this be achieved? I'm completely out of ideas.

Thanks.


回答1:


try to attach listener on both touchend and touchcancel.

$(document).on('touchend touchcancel', function() {
    doSomthing();
});



回答2:


I wrote a shim to deal with this problem Probably a bit late for you but it might help someone. https://github.com/TNT-RoX/android-swipe-shim



来源:https://stackoverflow.com/questions/19088117/touchend-not-firing-after-touchmove

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!