Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created April 3, 2024 06:17
Show Gist options
  • Save barneycarroll/7d4e0eaa307acfb46708a73d810bc7a5 to your computer and use it in GitHub Desktop.
Save barneycarroll/7d4e0eaa307acfb46708a73d810bc7a5 to your computer and use it in GitHub Desktop.
Selectively disable mobile browser zoom on input focus
// Disable mobile zoom on focus
{
// Customise the selector value as necessary
const selector = '[data-nozoom]'
const $viewport =
document.querySelector('meta[name=viewport]')
??
Object.assign(document.createElement('meta'), {name: 'viewport'})
const $scale0 = Object.assign($viewport.cloneNode(), {content: 'user-scalable=0'})
const $scale1 = Object.assign($viewport.cloneNode(), {content: 'user-scalable=1'})
addEventListener('mousedown', async e => {
if(!e.target.matches(selector))
return
$scale1.remove()
document.head.append($scale0)
await new Promise(done => setTimeout(done, 200))
$scale0.remove()
document.head.append($scale1)
}, {
passive: true
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment