93 lines
2.3 KiB
JavaScript
93 lines
2.3 KiB
JavaScript
/**
|
|
* FastClick event implementation. (removes 300ms delay on touch devices)
|
|
* Based on https://developers.google.com/mobile/articles/fast_buttons
|
|
*
|
|
* You may use it outside the Magnific Popup by calling just:
|
|
*
|
|
* $('.your-el').mfpFastClick(function() {
|
|
* console.log('Clicked!');
|
|
* });
|
|
*
|
|
* To unbind:
|
|
* $('.your-el').destroyMfpFastClick();
|
|
*
|
|
*
|
|
* Note that it's a very basic and simple implementation, it blocks ghost click on the same element where it was bound.
|
|
* If you need something more advanced, use plugin by FT Labs https://github.com/ftlabs/fastclick
|
|
*
|
|
*/
|
|
|
|
(function() {
|
|
var ghostClickDelay = 1000,
|
|
supportsTouch = 'ontouchstart' in window,
|
|
unbindTouchMove = function() {
|
|
_window.off('touchmove'+ns+' touchend'+ns);
|
|
},
|
|
eName = 'mfpFastClick',
|
|
ns = '.'+eName;
|
|
|
|
|
|
// As Zepto.js doesn't have an easy way to add custom events (like jQuery), so we implement it in this way
|
|
$.fn.mfpFastClick = function(callback) {
|
|
|
|
return $(this).each(function() {
|
|
|
|
var elem = $(this),
|
|
lock;
|
|
|
|
if( supportsTouch ) {
|
|
|
|
var timeout,
|
|
startX,
|
|
startY,
|
|
pointerMoved,
|
|
point,
|
|
numPointers;
|
|
|
|
elem.on('touchstart' + ns, function(e) {
|
|
pointerMoved = false;
|
|
numPointers = 1;
|
|
|
|
point = e.originalEvent ? e.originalEvent.touches[0] : e.touches[0];
|
|
startX = point.clientX;
|
|
startY = point.clientY;
|
|
|
|
_window.on('touchmove'+ns, function(e) {
|
|
point = e.originalEvent ? e.originalEvent.touches : e.touches;
|
|
numPointers = point.length;
|
|
point = point[0];
|
|
if (Math.abs(point.clientX - startX) > 10 ||
|
|
Math.abs(point.clientY - startY) > 10) {
|
|
pointerMoved = true;
|
|
unbindTouchMove();
|
|
}
|
|
}).on('touchend'+ns, function(e) {
|
|
unbindTouchMove();
|
|
if(pointerMoved || numPointers > 1) {
|
|
return;
|
|
}
|
|
lock = true;
|
|
e.preventDefault();
|
|
clearTimeout(timeout);
|
|
timeout = setTimeout(function() {
|
|
lock = false;
|
|
}, ghostClickDelay);
|
|
callback();
|
|
});
|
|
});
|
|
|
|
}
|
|
|
|
elem.on('click' + ns, function() {
|
|
if(!lock) {
|
|
callback();
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
$.fn.destroyMfpFastClick = function() {
|
|
$(this).off('touchstart' + ns + ' click' + ns);
|
|
if(supportsTouch) _window.off('touchmove'+ns+' touchend'+ns);
|
|
};
|
|
})(); |