JS监控滑动触摸屏事件的方法
本文介绍了js实现滑动触摸屏事件监控的方法,供大家参考,具体实现方法如下:功能span_move_fun(){
VaR跨度= document.getelementbyid(move_k );
无功span_left = $(跨)。偏移(左);
无功span_top = $(跨)。偏移(顶部);
无功start_left = $(跨)。偏移(左);
无功start_top = $(跨)。偏移(顶部);
Span.addEventListener('touchstart功能(事件){
Event.preventDefault();
如果(event.targettouches.length = = 1){
VaR触摸=事件。targettouches { 0 };
span.style.position =绝对;
span_top = $(这)。偏移(顶部);
span_left = $(这)。偏移(左);
start_top = touch.pagey
start_left = touch.pagex
var = parseFloat(左touch.pagex - start_left + span_left-30);
var = parseFloat(touch.pagey - start_top + span_top-73);
span.style.left =字符串(左)+ 'px;
span.style.top =字符串(上)+ 'px;
}
});
Span.addEventListener('touchmove功能(事件){
Event.preventDefault();
如果(event.targettouches.length = = 1){
VaR触摸=事件。targettouches { 0 };
span.style.position =绝对;
var = parseFloat(左touch.pagex - start_left + span_left-30);
var = parseFloat(touch.pagey - start_top + span_top-73);
span.style.left =字符串(左)+ 'px;
span.style.top =字符串(上)+ 'px;
}
});
Span.addEventListener('touchend功能(事件){
VaR触摸=事件。changedtouches { 0 };
如果(parseFloat(touch.pagex - start_left + span_left-30 = 620)| | parseFloat(touch.pagey - start_top + span_top-73)= 587){
span.style.left =字符串(span_left-30)+ 'px;
span.style.top =字符串(span_top-73)+ 'px;
}
event.stoppropagation();
});
}
JS的左、右滑动触摸事件主要有三:touchstart touchmove touchend事件,,。这三个事件的最重要的属性是pagex和pagey,代表X和Y坐标。
touchstart触发触摸的启动事件
在触摸结束触发touchend事件
touchmove事件是奇怪的。刺激触觉的过程这一事件它是正确的,但在我的Android 1.5,经过touchstart激发,它会刺激一次,其余的几乎和touchend相同。
这三个事件的时间戳属性,看时间戳属性,你可以看到订单touchstart touchmove -> -> touchmove -> -> ->…touchmove touchend。
下面是一个代码实例:
document.getelementsbytagname_r('body){ 0 }。addEventListener('touchstart功能(e){)
nstarty = e.targettouches { 0 }。pagey;
nstartx = e.targettouches { 0 }。pagex;
});
document.getelementsbytagname_r('body){ 0 }。addEventListener('touchend功能(e){)
nchangy = e.changedtouches { 0 }。pagey;
nchangx = e.changedtouches { 0 }。pagex;
});
PS:
1。触摸事件和点击事件不同时触发,现在移动设备更好,这个问题已经完全避免。
2。注意位移的开始和结束的接触。如果位移小于小不应该做任何动作。
希望本文能对大家的javascript程序设计有所帮助。