您的位置:首页 - 教程 - 网页特效 - 正文
移动端div块跟随手指拖动
效果体验:
http://hovertree.com/texiao/mobile/14/

请使用触屏手机访问该页面。移动端div块跟随手指拖动,pc端div根据鼠标拖动请用mousemove等事件

代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
<script type="text/javascript" src="http://down.hovertree.com/jquery/jquery-1.9.1.min.js"></script>
<title>移动端div块跟随手指拖动_何问起</title><base target="_blank" />
</head>
<body>
<div><h3>div块跟随手指拖动</h3></div>
<div id="idhovertree">请使用触屏手机访问本页,按住本方块拖动。</div>
<div style="position:relative;margin-top:290px;" >
手机扫描下面二维码访问本页:
<br /> <img src="http://hovertree.com/hvtimg/bjagja/h0pgqkht.png" />
<a href="http://hovertree.com/h/bjag/nad584dn.htm">说明</a>
<a href="http://hovertree.com">首页</a>
</div>
<style type="text/css">
div#idhovertree {
width: 200px;
height: 200px;
background: #ccc;
position: absolute;
left: 0px;
top: 100px;
z-index:9;
}
</style>
<script type="text/javascript">
var _x_start,_y_start,_x_move,_y_move,_x_end,_y_end,left_start,top_start;
document.getElementById("idhover"+"tree").addEventListener("touchstart", function (e)
{

_x_start=e.touches[0].pageX;
_y_start=e.touches[0].pageY;
// console.log("start",_x_start)
left_start = $("#idhovertree").css("left");
top_start = $("#idhovertree").css("top");

})
document.getElementById("idhovertree").addEventListener("touchmove", function (e)
{
event.preventDefault();
_x_move=e.touches[0].pageX;
_y_move=e.touches[0].pageY;
// console.log("move",_x_move)
$("#idhovertree").css("left", parseFloat(_x_move) - parseFloat(_x_start) + parseFloat(left_start) + "px");
$("#idhovertree").css("top", parseFloat(_y_move) - parseFloat(_y_start) + parseFloat(top_start) + "px");
console.log(parseFloat(_y_move)-parseFloat(_y_start)+parseFloat(top_start));
})
document.getElementById("idho"+"vertree").addEventListener("touchend",function(e)
{
var _x_end=e.changedTouches[0].pageX;
var _y_end=e.changedTouches[0].pageY;
// console.log("end",_x_end)
})
//阻止浏览器下拉事件
$('body').on('touchmove', function (event) {event.preventDefault();});
// or
// document.addEventListener('touchmove', function(e){e.preventDefault()}, false);

</script>
</body>
</html>

参考:http://hovertree.com/h/bjaf/0k3j0n95.htm

评论: