<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style type="text/Css">
body{background-color:#000000;}
.window{position:absolute;z-index:1;overflow:hidden;width:100%;height:400px;background-color:red;left: 0px;}
.dragme{position:relative;background-image:url('img/testbg.png');width:100%;height:400px;}
</style>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
var isdrag=false;
var tx,x;
$(function(){
document.getElementById("tap").addEventListener('touchend',function(){
isdrag = false;
});
document.getElementById("tap").addEventListener('touchstart',selectmouse);
document.getElementById("tap").addEventListener('touchmove',movemouse);
});
function movemouse(e){
if (isdrag){
var n = tx + e.touches[0].pageX - x;
$("#tap").css("left",n);
return false;
}
}
function selectmouse(e){
isdrag = true;
tx = parseInt(document.getElementById("tap").style.left+0);
x = e.touches[0].pageX;
return false;
}
// var isdrag=false;
// var tx,x,ty,y;
// $(function(){
// document.getElementById("tap").addEventListener('touchend',function(){
// isdrag = false;
// });
// document.getElementById("tap").addEventListener('touchstart',selectmouse);
// document.getElementById("tap").addEventListener('touchmove',movemouse);
// });
// function movemouse(e){
// if (isdrag){
// var n = tx + e.touches[0].pageX - x;
// m = ty + e.touches[0].pageY - y;
// $("#tap").css({"left":n,"top":m});
// return false;
// }
// }
// function selectmouse(e){
// isdrag = true;
// tx = parseInt(document.getElementById("tap").style.left+0);
// ty = parseInt(document.getElementById("tap").style.top+0);
// x = e.touches[0].pageX;
// y = e.touches[0].pageY;
// return false;
// }
</script>
</head>
<body>
<div align="left" class="window">
<div id="tap" class="dragme">
这是一个可以通过触摸屏拖动的demo<br>
这个demo花费了我半天时间,原因是以前从来没有做过面向触摸屏的Web,按说mousedown,mouseup,mousemove和touchstart,touchend,touchmove
之间是可以互通的,也就是说一般面向pc开发的mouse时间对touch事件有效,据说是效率有差异。但是pc上测试没有任何问题,在手机上就是无效。
然后……
然后百度了很久很久……
</div>
</div>
</html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style type="text/Css">body {height: 2000px;}#block {width:200px;height:200px;background-color: red;position: absolute;left: 0;top: 0;}</style>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
window.οnlοad=function(){
// 获取节点
var block = document.getElementById("block");
var oW,oH;
// 绑定touchstart事件
block.addEventListener("touchstart", function(e) {
console.log(e);
var touches = e.touches[0];
oW = touches.clientX - block.offsetLeft;
oH = touches.clientY - block.offsetTop;
//阻止页面的滑动默认事件
document.addEventListener("touchmove",defaultEvent,false);
},false)
block.addEventListener("touchmove", function(e) {
var touches = e.touches[0];
var oLeft = touches.clientX - oW;
var oTop = touches.clientY - oH;
if(oLeft < 0) {
oLeft = 0;
}else if(oLeft > document.documentElement.clientWidth - block.offsetWidth) {
oLeft = (document.documentElement.clientWidth - block.offsetWidth);
}
block.style.left = oLeft + "px";
block.style.top = oTop + "px";
},false);
block.addEventListener("touchend",function() {
document.removeEventListener("touchmove",defaultEvent,false);
},false);
function defaultEvent(e) {
e.preventDefault();
}
}
</script>
</head>
<body>
<div>
touchstart,touchmove,
touchend,touchcancel
</div>
<div id="block"></div>
</html>