public
Created

Fix for jQuery Mobile's scrolling and taphold behavior

  • Download Gist
fixScrollAndTapHold.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
(function fixScrollAndTapHold() {
 
var $document = $(document);
var isScrolling = false;
var lastX, lastY, lastT, scrollTimeout;
 
$document.bind("touchstart mousedown click", function(e) {
lastT = e.originalEvent.timeStamp;
});
 
$document.bind("scroll", function(e) {
var t = e.originalEvent.timeStamp;
 
if (!t) {
return;
}
 
var x = window.pageXOffset;
var y = window.pageYOffset;
 
var dT = t - lastT;
var dX = Math.abs(x - lastX);
var dY = Math.abs(y - lastY);
var dMax = Math.max(dX, dY);
 
lastT = t;
lastX = x;
lastY = y;
 
var speed = dMax / dT;
 
if (speed >= 0.1 || dMax >= 10) {
isScrolling = true;
}
 
clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(function() {
isScrolling = false;
}, 500);
});
 
/**
* @return {Boolean}
*/
$.mobile.isScrolling = function() {
return isScrolling;
};
 
})();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.