Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A jQuery plugin to selectively disable the iOS double-tap-to-zoom action on specific page elements (and have that generate two click events instead).
// jQuery no-double-tap-zoom plugin
// Triple-licensed: Public Domain, MIT and WTFPL license - share and enjoy!
(function($) {
var IS_IOS = /iphone|ipad/i.test(navigator.userAgent);
$.fn.nodoubletapzoom = function() {
if (IS_IOS)
$(this).bind('touchstart', function preventZoom(e) {
var t2 = e.timeStamp
, t1 = $(this).data('lastTouch') || t2
, dt = t2 - t1
, fingers = e.originalEvent.touches.length;
$(this).data('lastTouch', t2);
if (!dt || dt > 500 || fingers > 1) return; // not double-tap
e.preventDefault(); // double tap - prevent the zoom
// also synthesize click events we just swallowed up
$(this).trigger('click').trigger('click');
});
};
})(jQuery);
@Thiemo-Mueller

This comment has been minimized.

Show comment Hide comment
@Thiemo-Mueller

Thiemo-Mueller Sep 2, 2013

Hey johan,

thanks for the snippet!
I'd just recommend adding a "return this;" to make it chainable.

--Thiemo

Hey johan,

thanks for the snippet!
I'd just recommend adding a "return this;" to make it chainable.

--Thiemo

@john-cheesman

This comment has been minimized.

Show comment Hide comment
@john-cheesman

john-cheesman Feb 20, 2014

Thanks for this!

Thanks for this!

@EntropyDivideByZero

This comment has been minimized.

Show comment Hide comment
@EntropyDivideByZero

EntropyDivideByZero Mar 19, 2014

Thank you for this. I am using the presses of the button to call a function to increment a counter. I need the double taps to call my function twice. Might it be OK for me to call $(this).trigger('click').trigger('click'); twice in the section // also synthesize click events we just swallowed up.

Thank you again.

Thank you for this. I am using the presses of the button to call a function to increment a counter. I need the double taps to call my function twice. Might it be OK for me to call $(this).trigger('click').trigger('click'); twice in the section // also synthesize click events we just swallowed up.

Thank you again.

@tam-vo

This comment has been minimized.

Show comment Hide comment
@tam-vo

tam-vo Apr 29, 2014

Thanks for your idea, I need to port this snippet to javascript.

tam-vo commented Apr 29, 2014

Thanks for your idea, I need to port this snippet to javascript.

@Legomite

This comment has been minimized.

Show comment Hide comment
@Legomite

Legomite Jul 28, 2014

It dosnt seem to work?

It dosnt seem to work?

@shlomitc

This comment has been minimized.

Show comment Hide comment
@shlomitc

shlomitc Aug 3, 2015

Works great, thanks a lot!
I suggest lowering the time between the two taps because it prevents fast scrolling of the page.
200ms should be enough.

shlomitc commented Aug 3, 2015

Works great, thanks a lot!
I suggest lowering the time between the two taps because it prevents fast scrolling of the page.
200ms should be enough.

@leizard

This comment has been minimized.

Show comment Hide comment
@leizard

leizard Nov 11, 2015

Definitely save my day. thank you so much

leizard commented Nov 11, 2015

Definitely save my day. thank you so much

@karthickibz

This comment has been minimized.

Show comment Hide comment
@karthickibz

karthickibz Jun 22, 2017

it is not working, i just check my iphone 7plus moblie.double tap to zoom still working on my mobile

it is not working, i just check my iphone 7plus moblie.double tap to zoom still working on my mobile

@futuretap

This comment has been minimized.

Show comment Hide comment
@futuretap

futuretap Aug 24, 2017

I think it should be $(this).trigger('click') at the end, not $(this).trigger('click').trigger('click'). Otherwise, I receive 3 clicks for one double-tap.

futuretap commented Aug 24, 2017

I think it should be $(this).trigger('click') at the end, not $(this).trigger('click').trigger('click'). Otherwise, I receive 3 clicks for one double-tap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment