
flash 3.0 怎样拖动一个元件到指定位置,如果拖动到了,就放下,没到就回到原位! 10
2个回答
展开全部
<!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="jquery-3.1.1.min.js"></script>
<title>div块跟随手指拖动</title>
</head>
<body>
<div class="id"></div>
<style type="text/css">
div{
width: 200px;
height: 200px;
background: #ccc;
position: absolute;
left: 0px;
top: 100px;
}
</style>
<script type="text/javascript">
var _x_start,_y_start,_x_move,_y_move,_x_end,_y_end,left_start,top_start;
document.getElementsByClassName("id").addEventListener("touchstart",function(e){
_x_start=e.touches[0].pageX;
_y_start=e.touches[0].pageY;
// console.log("start",_x_start)
left_start=$(".id").css("left");
top_start=$(".id").css("top");
})
document.getElementsByClassName("id")[0].addEventListener("touchmove",function(e)
{
_x_move=e.touches[0].pageX;
_y_move=e.touches[0].pageY;
// console.log("move",_x_move)
$(".id").css("left",parseFloat(_x_move)-parseFloat(_x_start)+parseFloat(left_start)+"px");
$(".id").css("top",parseFloat(_y_move)-parseFloat(_y_start)+parseFloat(top_start)+"px");
// console.log(parseFloat(_y_move)-parseFloat(_y_start)+parseFloat(top_start));
console.log(parseFloat(_x_move)-parseFloat(_x_start)+parseFloat(left_start));
})
document.getElementsByClassName("id")[0].addEventListener("touchend",function(e)
{
console.log(left_start)
if(parseFloat(_x_move)-parseFloat(_x_start)+parseFloat(left_start)>200){
$(".id").css("left",parseFloat(_x_move)-parseFloat(_x_start)+parseFloat(left_start)+"px");
$(".id").css("top",parseFloat(_y_move)-parseFloat(_y_start)+parseFloat(top_start)+"px");
}else{
$(".id").css("left",parseFloat(left_start)+"px");
$(".id").css("top",parseFloat(top_start)+"px");
}
// console.log("end",_x_end)
})
//阻止浏览器下拉事件
$('body').on('touchmove', function (event) {event.preventDefault();});
// or
// document.addEventListener('touchmove', function(e){e.preventDefault()}, false);
</script>
</body>
</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="jquery-3.1.1.min.js"></script>
<title>div块跟随手指拖动</title>
</head>
<body>
<div class="id"></div>
<style type="text/css">
div{
width: 200px;
height: 200px;
background: #ccc;
position: absolute;
left: 0px;
top: 100px;
}
</style>
<script type="text/javascript">
var _x_start,_y_start,_x_move,_y_move,_x_end,_y_end,left_start,top_start;
document.getElementsByClassName("id").addEventListener("touchstart",function(e){
_x_start=e.touches[0].pageX;
_y_start=e.touches[0].pageY;
// console.log("start",_x_start)
left_start=$(".id").css("left");
top_start=$(".id").css("top");
})
document.getElementsByClassName("id")[0].addEventListener("touchmove",function(e)
{
_x_move=e.touches[0].pageX;
_y_move=e.touches[0].pageY;
// console.log("move",_x_move)
$(".id").css("left",parseFloat(_x_move)-parseFloat(_x_start)+parseFloat(left_start)+"px");
$(".id").css("top",parseFloat(_y_move)-parseFloat(_y_start)+parseFloat(top_start)+"px");
// console.log(parseFloat(_y_move)-parseFloat(_y_start)+parseFloat(top_start));
console.log(parseFloat(_x_move)-parseFloat(_x_start)+parseFloat(left_start));
})
document.getElementsByClassName("id")[0].addEventListener("touchend",function(e)
{
console.log(left_start)
if(parseFloat(_x_move)-parseFloat(_x_start)+parseFloat(left_start)>200){
$(".id").css("left",parseFloat(_x_move)-parseFloat(_x_start)+parseFloat(left_start)+"px");
$(".id").css("top",parseFloat(_y_move)-parseFloat(_y_start)+parseFloat(top_start)+"px");
}else{
$(".id").css("left",parseFloat(left_start)+"px");
$(".id").css("top",parseFloat(top_start)+"px");
}
// console.log("end",_x_end)
})
//阻止浏览器下拉事件
$('body').on('touchmove', function (event) {event.preventDefault();});
// or
// document.addEventListener('touchmove', function(e){e.preventDefault()}, false);
</script>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询