js基本touch事件
2015-08-06 15:11
519 查看
正常情况下,一次触摸会按照这样的顺序触发事件:
touchstart
mousedown
touchmove
mousemove
touchend
mouseup
click
可以使用e.preventDefault()阻止事件冒泡
例子:
光点跟随手指移动
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width , initial-scale=1 , maximum-scale=1 , user-scalable=no">
<style>
body{background-color:#222;}
.spot{position:absolute; width:70px; height:70px; border-radius:35px; box-shadow:0px 0px 40px #fff; background-color:#fff; opacity:.7;}
</style>
</head>
<body>
<script>
var spot = null;
document.addEventListener('touchstart',function(e){
//touch所有类型事件都会冒泡,如果阻止了touchstart的默认行为,后续的mousedown和click事件将不会触发
e.preventDefault();
//如果已经生成小光点了,就直接返回
if(spot){
return;
}
spot = document.createElement('div');
spot.classList.add('spot');
spot.style.top = e.touches[0].pageY - 35;
spot.style.left = e.touches[0].pageX - 35;
document.body.appendChild(spot);
},false);
document.addEventListener('touchmove',function(e){
//阻止了touchmove的默认行为,后续的mousemove事件将不会触发
e.preventDefault();
if(spot){
spot.style.top = e.touches[0].pageY - 35;
spot.style.left = e.touches[0].pageX - 35;
}
});
document.addEventListener('touchend',function(e){
//如果阻止了touchend的默认行为,后续的mouseup和click事件将不会触发
e.preventDefault();
if(spot){
//删除这个光点
document.body.removeChild(spot);
spot = null;
}
})
</script>
</body>
</html>
注意:
touches表示当前位于屏幕上的所有手指动作的列表,是一个TouchList类型的对象,TouchList是一个类数组对象,它里面装的是Touch对象
关于坐标:
clientX/Y:触摸点相对于浏览器窗口viewport的位置
pageX/Y:触摸点相对于页面的位置
screenX/Y:触摸点相对于屏幕的位置--通常来说和clientX/Y在计算时的区别就是少了一个状态栏和地址栏
touchstart
mousedown
touchmove
mousemove
touchend
mouseup
click
可以使用e.preventDefault()阻止事件冒泡
例子:
光点跟随手指移动
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width , initial-scale=1 , maximum-scale=1 , user-scalable=no">
<style>
body{background-color:#222;}
.spot{position:absolute; width:70px; height:70px; border-radius:35px; box-shadow:0px 0px 40px #fff; background-color:#fff; opacity:.7;}
</style>
</head>
<body>
<script>
var spot = null;
document.addEventListener('touchstart',function(e){
//touch所有类型事件都会冒泡,如果阻止了touchstart的默认行为,后续的mousedown和click事件将不会触发
e.preventDefault();
//如果已经生成小光点了,就直接返回
if(spot){
return;
}
spot = document.createElement('div');
spot.classList.add('spot');
spot.style.top = e.touches[0].pageY - 35;
spot.style.left = e.touches[0].pageX - 35;
document.body.appendChild(spot);
},false);
document.addEventListener('touchmove',function(e){
//阻止了touchmove的默认行为,后续的mousemove事件将不会触发
e.preventDefault();
if(spot){
spot.style.top = e.touches[0].pageY - 35;
spot.style.left = e.touches[0].pageX - 35;
}
});
document.addEventListener('touchend',function(e){
//如果阻止了touchend的默认行为,后续的mouseup和click事件将不会触发
e.preventDefault();
if(spot){
//删除这个光点
document.body.removeChild(spot);
spot = null;
}
})
</script>
</body>
</html>
注意:
touches表示当前位于屏幕上的所有手指动作的列表,是一个TouchList类型的对象,TouchList是一个类数组对象,它里面装的是Touch对象
关于坐标:
clientX/Y:触摸点相对于浏览器窗口viewport的位置
pageX/Y:触摸点相对于页面的位置
screenX/Y:触摸点相对于屏幕的位置--通常来说和clientX/Y在计算时的区别就是少了一个状态栏和地址栏
相关文章推荐
- js多点触摸touch事件
- js数组forEach map filter方法
- js的arguments用法
- js的shift()方法
- JS实现可直接显示网页代码运行效果的HTML代码预览功能实例
- javascript的一些小知识2
- JavaScript常用语句
- Javascript 五个特性介绍
- js实现简单的秒表走动的时钟特效
- Air中读取JavaScript的方法
- [Javascript] 函数参数讨论
- JS封装的几个位操作函数和16进制操作函数
- JS判断,今天所在季度,第几周, 季度的第几周,年度第几周
- 编写良好javascript,css,html的方法技巧(持续更新……)
- 使用js处理图片效
- 30天了解30种技术系列---(1)现代web应用服务器-Express.js
- Extjs radiogroup 获取选中
- 整理 Anglarjs的service,directve的基本用法
- Extjs textfield 宽
- js运动基础