Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safari iPhone - How to detect zoom level and offset?

Tags:

I looking for options on how to track user zooming and panning on a page when viewed in Safari on an iPhone. Safari exposes move and gesture events, so theoretically I can keep a running tally of pan and zoom operations, but that seems like overkill since the browser must track that internally.

Is this information exposed through the Document Object Model?

like image 361
Jason Kester Avatar asked Mar 21 '09 00:03

Jason Kester


People also ask

How do I see the zoom level in Safari?

Make all content larger: Choose Safari > Settings for [website] and choose an option from the Page Zoom pop-up menu, or choose View > Zoom In. You can also pinch open on a trackpad that supports gestures. Safari remembers the zoom level when you return to the website (unless you pinch open to zoom).

How do I fix the zoom on my iPhone Safari?

Adjust the magnification: Double-tap the screen with three fingers (without lifting your fingers after the second tap), then drag up or down. Or triple-tap with three fingers, then drag the Zoom Level slider.

How do I check my browser zoom level?

By default, Chrome sets the zoom level to 100%. To manually adjust the settings, use the Ctrl key and “+” or “-” combos to increase or decrease the page magnification.

Why does Safari keep zooming in?

Sounds like you have system zoom enabled. You can disable it from System Settings > Accessibility > Zoom.


2 Answers

When you zoom in, window.innerWidth is adjusted, but document.documentElement.clientWidth is not, therefore:

var zoom = document.documentElement.clientWidth / window.innerWidth; 

(I've tested iOS4, without viewport <meta>).

However, I wouldn't rely on it for anything important. DOM viewport sizes/pixel sizes in mobile browsers are a complete mess.

like image 166
Kornel Avatar answered Oct 15 '22 19:10

Kornel


On Mobile Safari and Android, here is an accurate way to measure how much the page has been zoomed.

Try it here: http://jsbin.com/cobucu/3 - change zoom then click measure.

Technique is to add a top level div:

<body> <div id=measurer style="position:absolute;width:100%"></div> 

and use the calculation:

function getZoom(){     return document.getElementById('measurer').offsetWidth / window.innerWidth; } 

The only problem is finding a tidy way to detect that the user has changed zoom (pinch, double tap, etc). Options:

  • webkitRequestAnimationFrame: very reliable, but likely to cause jankiness if using animations (due to performance hit)
  • setInterval: reliable but very ugly
  • touch events: look for two fingers or double tap: ugly and maybe difficult to make 100% reliable
  • window.onresize + window.onorientationchange + window.onscroll: simple but totally unreliable (Edit: and onscroll can cause performance problems in WKWebView or Mobile Safari 8 or greater).

PS: Windows Phone needs a different solution (pinch-zoom doesn't change the viewport - pinch-zoom on Windows has its own separate viewport that is not visible to javascript).

Edit: Android Visual Viewport resize and scroll events may help? See https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport#Events

like image 24
robocat Avatar answered Oct 15 '22 20:10

robocat