1.手指事件:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,user-scalable=no" />
<title></title>
<style>
#box{
width: 200px;
height: 200px;
background: green;
color: #fff;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
document.addEventListener('touchstart',function(ev){
ev.preventDefault();
});
var box=document.getElementById("box");
box.addEventListener('touchstart',function(){
this.innerHTML='手指按下了';
});
box.addEventListener('touchmove',function(){
this.innerHTML='手指移动了';
});
box.addEventListener('touchend',function(){
this.innerHTML='手指离开了';
});
</script>
</body>
</html>
2.touches当前屏幕中的手指列表:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,user-scalable=no" />
<title></title>
<style>
#box{
width: 200px;
height: 200px;
background: green;
color: #fff;
font-size: 50px;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
var box=document.getElementById("box");
box.addEventListener('touchstart',function(ev){
this.innerHTML=ev.touches.length;
});
</script>
</body>
</html>
3.重力加速度事件:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,user-scalable=no" />
<title></title>
<style>
#box{
width: 200px;
height: 200px;
background: green;
color: #fff;
position: absolute;
left: 50px;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
var box=document.getElementById("box");
window.addEventListener('devicemotion',function(ev){
var motion=ev.accelerationIncludingGravity;
var x=parseFloat(getComputedStyle(box).left);
box.style.left=x+motion.x+'px';
});
</script>
</body>
</html>
4.手机倾斜事件:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,user-scalable=no" />
<title></title>
<style>
#box{
width: 200px;
height: 200px;
background: green;
color: #fff;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
var box=document.getElementById("box");
window.addEventListener('deviceorientation',function(ev){
var motion=ev.accelerationIncludingGravity;
box.innerHTML=`x轴倾斜:${ev.beta.toFixed(1)}</br>y轴倾斜:${ev.gamma.toFixed(1)}</br>z轴倾斜:${ev.alpha.toFixed(1)}`;
});
</script>
</body>
</html>
5.手机摇一摇:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,user-scalable=no" />
<title></title>
</head>
<body>
<script>
var lastRange=0;
var isShake=false;
window.addEventListener('devicemotion',function(ev){
var motion=ev.accelerationIncludingGravity;
var x=Math.abs(motion.x);
var y=Math.abs(motion.y);
var z=Math.abs(motion.z);
var range=x+y+z;
if(range-lastRange>100){
isShake=true;
}
if(isShake && range<50){
alert('摇一摇了');
isShake=false;
}
});
</script>
</body>
</html>
6.多指旋转:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,user-scalable=no" />
<title></title>
<style>
#box{
width: 200px;
height: 200px;
background: green;
color: #fff;
font-size: 50px;
}
</style>
</head>
<body>
<div id="box">kaivon</div>
<script>
document.addEventListener('touchstart',function(ev){
ev.preventDefault();
});
document.addEventListener('touchmove',function(ev){
ev.preventDefault();
});
var box=document.getElementById("box");
var startDeg=0;
box.addEventListener('gesturestart',function(){
this.style.background='blue';
if(this.style.transform){
startDeg=parseFloat(this.style.transform.split('(')[1]);
}
});
box.addEventListener('gesturechange',function(ev){
this.style.transform='rotate('+(ev.rotation+startDeg)+'deg)';
});
box.addEventListener('gestureend',function(){
this.style.background='green';
});
</script>
</body>
</html>
7.多指缩放:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,user-scalable=no" />
<title></title>
<style>
#box{
width: 200px;
height: 200px;
background: green;
color: #fff;
font-size: 50px;
position: absolute;
left: 70px;
top: 100px;
}
</style>
</head>
<body>
<div id="box">kaivon</div>
<script>
document.addEventListener('touchstart',function(ev){
ev.preventDefault();
});
document.addEventListener('touchmove',function(ev){
ev.preventDefault();
});
var box=document.getElementById("box");
var startScale=1;
box.addEventListener('gesturestart',function(){
this.style.background='blue';
if(this.style.transform){
startScale=parseFloat(this.style.transform.split('(')[1]);
}
});
box.addEventListener('gesturechange',function(ev){
var sc=ev.scale*startScale;
sc=sc<0.5?0.5:sc;
this.style.transform='scale('+sc+')';
});
box.addEventListener('gestureend',function(){
this.style.background='green';
});
</script>
</body>
</html>