Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect mobile browser pinch zoom

I want to know if there is a way to detect user is doing pinch zoom.

I want to do two things when user zooming in:

  1. let the div align the left side of screen
  2. set the width of div equal to the viewport width

I found Android has touchstart, touchend and touchmove, but they won't be triggered during multitouch. I want to complete above function right after finishing zooming.

Are there any other approaches?


I am not doing the web app. I am just adding some functions to the mobile version.

like image 258
Roger Chan Avatar asked Dec 04 '25 21:12

Roger Chan


1 Answers

Android browser supports touchstart, touchend & touchmove with JavaScript, but the problem with older androids is the number of touches these events are detecting.

For example, this code will log the message on IOS and on newer android devices:

var obj = document.getElementById('elmId');
obj.addEventListener('touchmove', function(event) {
  if (event.targetTouches.length == 2) {
    console.log("exactly 2 fingers gesture inside elmId ");
  }
}, false);

Older androids will do nothing because event.targetTouches.length will never be equal to 2.

IMHO, you should use this approach that will support most devices and provide a fallback option for older devices (use other gestures for zooming like double tap or a button to zoom).

like image 127
AssafDamari Avatar answered Dec 07 '25 10:12

AssafDamari