标签:def var auto style on() selector odi down doctype
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>移动端双指缩放、旋转</title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0,maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<style>
#div1{
width:200px;
height:200px;
background: red;
margin: 50px auto;
font-size: 40px;
color: #fff;
}
</style>
<script>
function a2d(n){
return n*180/Math.PI;
}
document.addEventListener(‘DOMContentLoaded‘,function(){
var oDiv=document.querySelector(‘#div1‘);
var x=0;
var y=0;
var d=0;
var s=1;
oDiv.addEventListener(‘touchstart‘,function(ev){
var oldX=x;
var oldY=y;
var oldD=d;
var oldS=s;
function getT(ev){
var x1=ev.targetTouches[0].pageX;
var y1=ev.targetTouches[0].pageY;
var x2=ev.targetTouches[1].pageX;
var y2=ev.targetTouches[1].pageY;
var x=(x1+x2)/2;
var y=(y1+y2)/2;
return {x:x, y:y};
}
function getD(ev){
var x1=ev.targetTouches[0].pageX;
var y1=ev.targetTouches[0].pageY;
var x2=ev.targetTouches[1].pageX;
var y2=ev.targetTouches[1].pageY;
var x=x2-x1;
var y=y1-y2;
return a2d(Math.atan2(y,x));
}
function getS(ev){
var x1=ev.targetTouches[0].pageX;
var y1=ev.targetTouches[0].pageY;
var x2=ev.targetTouches[1].pageX;
var y2=ev.targetTouches[1].pageY;
var a=x1-x2;
var b=y1-y2;
return Math.sqrt(a*a+b*b);
}
if(ev.targetTouches.length==2){
var downX=getT(ev).x;
var downY=getT(ev).y;
var downD=getD(ev);
var downS=getS(ev);
}
document.addEventListener(‘touchmove‘,function(ev){
if(ev.targetTouches.length==2){
x=oldX+getT(ev).x-downX;
y=oldY+getT(ev).y-downY;
d=oldD+downD-getD(ev);
s=oldS*getS(ev)/downS;
oDiv.style.WebkitTransform=‘translate(‘+x+‘px,‘+y+‘px) scale(‘+s+‘) rotate(‘+d+‘deg)‘;
}
},false);
ev.preventDefault();
},false);
},false);
</script>
</head>
<body>
<div id="div1">DIV</div>
</body>
</html>
标签:def var auto style on() selector odi down doctype
原文地址:http://www.cnblogs.com/zhoufuwei/p/7647203.html