Explain Codes LogoExplain Codes Logo

Disable double-tap "zoom" option in browser on touch devices

web-development
responsive-design
performance
best-practices
Anton ShumikhinbyAnton Shumikhin·Oct 30, 2024
TLDR

Here's how to nip double-tap zoom in the bud: Add touch-action: manipulation; to your CSS. It blocks the zoom while keeping those pinch gestures nimble:

* { touch-action: manipulation; /* thinks twice before zooming */ }

Apply this to all elements (*) or pick and choose to maintain usability while preventing unintended zoom.

To totally rule out zooming on touch devices, add user-scalable=no to the viewport meta tag:

<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">

Divvying-up responsibilities with CSS classes

Finesse control over your elements' zoom behavior like a maestro wielding a baton. Assign elements needing a zoom overhaul a class named disable-dbl-tap-zoom:

.disable-dbl-tap-zoom { touch-action: manipulation; /* Stops zoom cold */ }

For buttons, interactive widgets, or any elements requiring special considerations, this class acts like a backstage pass.

Cancelling the zoom concert with jQuery

If an element is in a tapping frenzy and you don't want that to trigger zoom, jQuery can shush it:

$('.disable-dbl-tap-zoom').on('touchend', function(e) { var $this = $(this); if ($this.data('lastTouch') && e.timeStamp - $this.data('lastTouch') < 500) { e.preventDefault(); /* says 'not so fast' to double-tap zoom */ } $this.data('lastTouch', e.timeStamp); });

Just like bass beats in a song, this snippet rejects double-taps happening within a 500ms interval. No more accidental zooming ruining your browsing tempo!

Delivering the same groove across devices

Like any top hit song, you want it to sound great across all devices. So do a little compatibility dance across different browsers. And hey, don't forget about user experience! Test on small displays with low resolution, to big feature-packed top-performers.

Dance-offs, Zoom-outs, and User experience

Sure, disabling zoom can be a cool party trick, but if it's killing user experience, it's time for a reality check. Zoom should be an option where it truly enhances navigation. Also, nobody wants unexpected zooming in their face, so use this trick judiciously.

Crafting bespoke zoom-banishing spells with CSS

Sometimes you gotta play by the touch rules, but with a hint of rebellion. And that's where touch-action comes to your rescue. It's like a magic charm that decides which gestures to honor and which ones to ignore:

.card { touch-action: pan-x pan-y; /* greenlights panning, but pulls the plug on zoom */ }

So essentially, your elements get to do the samba (aka, users can swipe through cards), minus the accidental zoom conga line.

Memory lane: DancesiOS versus DancesAndroid

Like pop songs and their various remixes, browser behavior also remixes itself on different platforms. So, don't forget to drop your no-zoom party anthem on iOS and Android to see if the crowd there digs it.

Preempting double-tap zoom's encore with custom click triggers

When a tap decides to bring along a twin and they both insist on triggering zoom, you've got to stage a diversion. Here, timing is your friend:

let lastTap = 0; element.addEventListener('touchend', function(event) { let currentTime = new Date().getTime(); let tapLength = currentTime - lastTap; if (tapLength < 300 && tapLength > 0) { // Stage ordered diversion with custom click event event.preventDefault(); } lastTap = currentTime; });

When the second tap shows up within 300ms of the first, a custom action is fired and the double-tap zoom is banished for good!