Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  110] [ 1]  / answers: 1 / hits: 20055  / 11 Years ago, mon, september 30, 2013, 12:00:00

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.


More From » android

 Answers
44

try to attach listener on both touchend and touchcancel.



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

[#75347] Friday, September 27, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jackelyn

Total Points: 303
Total Questions: 103
Total Answers: 102

Location: Turks and Caicos Islands
Member since Sun, Mar 7, 2021
3 Years ago
;