标签:最大 targe oom word class turn 处理 || form
在触屏设备下,要判断是单指还是多指操作,可以通过event.touches数组对象的长度判断
例如:
document.addEventListener(‘touchstart‘,function(event){ var touches = event.touches; if(touches.length == 1){ console.log(‘单指‘); }else if(touches.length >=2){ console.log(‘多指‘); } })
实际上,要出发上面的多指事件,只能在同事触摸屏幕的时候才行。所以,双指事件的判断需要在touchmove事件中才行
document.addEventListener(‘touchmove‘, function (event) { if (event.touches && event.touches.length == 2) { console.log(‘双指在移动‘); } });
实例演示:
<img id="image" src="1.png"> var eleImg = document.querySelector(‘#image‘); var store = { scale: 1 }; // 缩放事件的处理 eleImg.addEventListener(‘touchstart‘, function (event) { var touches = event.touches; var events = touches[0]; var events2 = touches[1]; event.preventDefault(); // 第一个触摸点的坐标 store.pageX = events.pageX; store.pageY = events.pageY; store.moveable = true; if (events2) { store.pageX2 = events2.pageX; store.pageY2 = events2.pageY; } store.originScale = store.scale || 1; }); document.addEventListener(‘touchmove‘, function (event) { if (!store.moveable) { return; } event.preventDefault(); var touches = event.touches; var events = touches[0]; var events2 = touches[1]; // 双指移动 if (events2) { // 第2个指头坐标在touchmove时候获取 if (!store.pageX2) { store.pageX2 = events2.pageX; } if (!store.pageY2) { store.pageY2 = events2.pageY; } // 获取坐标之间的举例 var getDistance = function (start, stop) { return Math.hypot(stop.x - start.x, stop.y - start.y); }; // 双指缩放比例计算 var zoom = getDistance({ x: events.pageX, y: events.pageY }, { x: events2.pageX, y: events2.pageY }) / getDistance({ x: store.pageX, y: store.pageY }, { x: store.pageX2, y: store.pageY2 }); // 应用在元素上的缩放比例 var newScale = store.originScale * zoom; // 最大缩放比例限制 if (newScale > 3) { newScale = 3; } // 记住使用的缩放值 store.scale = newScale; // 图像应用缩放效果 eleImg.style.transform = ‘scale(‘+ newScale +‘)‘; } }); document.addEventListener(‘touchend‘, function () { store.moveable = false; delete store.pageX2; delete store.pageY2; }); document.addEventListener(‘touchcancel‘, function () { store.moveable = false; delete store.pageX2; delete store.pageY2; });
文章内容来自张大佬的内容;地址:https://www.zhangxinxu.com/wordpress/2020/06/mobile-event-touches-zoom-sacle/
他还有一个demo地址:https://www.zhangxinxu.com/study/202006/resize.html
标签:最大 targe oom word class turn 处理 || form
原文地址:https://www.cnblogs.com/chao202426/p/13278150.html