zepto touch 拖动元素,获取移动坐标

本文介绍了一个使用jQuery实现的触摸事件处理案例。该案例详细展示了如何通过JavaScript获取触摸开始(touchstart)、移动(touchmove)及结束(touchend)时的位置信息,并计算触摸过程中的坐标变化量。适用于移动设备上需要捕捉用户触摸操作的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


<div id="m" style="position:absolute;width: 100px;height: 100px;background: red"></div>

<script type="text/javascript">



var start_piont,end_point,delta_x,delta_y; 


$(document).ready(function(){
 
	$("#m").on("touchstart",function(e){
	    var touch = e.touches[0];  
	    start_piont = {  
	        x: touch.pageX,  
	        y: touch.pageY  
	    }
	    console.log(start_piont);
	    $("#m").css("top",touch.pageY);
	    $("#m").css("left",touch.pageX);
	}).bind('touchmove',function(e){
	    //在这里不断刷新
	     var touch = e.touches[0];  
	     end_point = {  
	         x: touch.pageX,  
	         y: touch.pageY  
	     }
	    console.log(end_point);
	    $("#m").css("top",touch.pageY);
	    $("#m").css("left",touch.pageX);
	}).bind('touchend', function(e){

	    //在这里取值计算
	    delta_x = end_point.x - start_piont.x;  
	    delta_y = end_point.y - start_piont.y; 

	    console.log(delta_x);
	    console.log(delta_y);
	});	
});

</script>	
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值