Detect mobile browser pinch zoom

社会主义新天地 提交于 2019-12-10 18:44:18

问题


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.


回答1:


Android has a ScaleGestureDetector. Here's an example. However, if you require additional multitouch support, e.g., rotation, I would recommend this library. I have used it personally (to store and retrieve transformation matrices of multiple images) and found it an excellent resource.




回答2:


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).



来源:https://stackoverflow.com/questions/9818336/detect-mobile-browser-pinch-zoom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!