检测手机上调用javascript函数的移动
我有一个网站,我想也能在手机上玩 在界面中,我主要使用javascript。如何检测用户手指在屏幕上的移动,以便调用正确的javascript函数 例如,在我的照片库中检测手机上调用javascript函数的移动,javascript,mobile,cross-browser,detection,Javascript,Mobile,Cross Browser,Detection,我有一个网站,我想也能在手机上玩 在界面中,我主要使用javascript。如何检测用户手指在屏幕上的移动,以便调用正确的javascript函数 例如,在我的照片库中 调用imagenext,以便画廊呈现下一幅图像 如果是移动电话,我该怎么做 onFingerMoved=“imagenext();” 或者更好的是,如何将移动事件添加到现有代码中?喜欢 onClick或onMove=function() 当然,我正在寻找跨移动浏览器的解决方案 请原谅我幼稚的例子,我是个乞丐 提前感谢您可以使用j
调用imagenext
,以便画廊呈现下一幅图像
如果是移动电话,我该怎么做
onFingerMoved=“imagenext();”
或者更好的是,如何将移动事件添加到现有代码中?喜欢
onClick或onMove=function()代码>
当然,我正在寻找跨移动浏览器的解决方案
请原谅我幼稚的例子,我是个乞丐
提前感谢您可以使用jQuery Mobile来完成此任务。这是他们对刷卡活动的看法。您可以按如下方式实现您正在尝试执行的操作:
$( "body" ).on( "swipe", function() {
imageNext();
);
你有ontouchstart,ontouchend,ontouchmove,你可以把它放在任何你喜欢的东西上
var _touches = {
'touchstart' : {'x' : -1, 'y' : -1}
, 'touchmove' : {'x' : -1, 'y' : -1}
, 'touchend' : false
, 'direction' : 'undetermined'
};
function touchHandler (event) {
var touch
, xSwipe
, ySwipe;
if (typeof event !== 'undefined'){
// for vanilla javascript use event.touches
if (typeof event.originalEvent.touches !== 'undefined') {
touch = event.originalEvent.touches[0];
switch (event.originalEvent.type) {
case 'touchstart':
case 'mousedown':
_touches[event.originalEvent.type].x = touch.pageX;
_touches[event.originalEvent.type].y = touch.pageY;
break;
case 'touchmove':
case 'mousemove':
_touches[event.originalEvent.type].x = touch.pageX;
_touches[event.originalEvent.type].y = touch.pageY;
break;
case 'touchend':
case 'mouseup':
_touches[event.originalEvent.type] = true;
if (_touches.touchstart.x > -1 && _touches.touchmove.x > -1) {
xSwipe = Math.abs(_touches.touchstart.x - _touches.touchmove.x);
ySwipe = Math.abs(_touches.touchstart.y - _touches.touchmove.y);
}
if (xSwipe > ySwipe && xSwipe > (getViewport().width * .33)) {
_touches.direction = _touches.touchstart.x < _touches.touchmove.x ? 'left' : 'right';
if (_touches.direction === 'left') {
// do something
} else if (_touches.direction === 'right') {
// do something
}
}
}
break;
}
}
}
}
$element.on('touchstart touchmove touchend', function (e) {
touchHandler(e);
});
var\u={
'touchstart':{'x':-1,'y':-1}
,'touchmove':{'x':-1,'y':-1}
,“touchend”:false
,“方向”:“未确定”
};
函数touchHandler(事件){
变压触控
,xSwipe
,ySwipe;
如果(事件类型!==“未定义”){
//对于普通javascript,请使用event.touchs
if(typeof event.originalEvent.touchs!=“未定义”){
touch=event.originalEvent.touch[0];
开关(event.originalEvent.type){
案例“touchstart”:
案例“mousedown”:
_触摸[event.originalEvent.type].x=touch.pageX;
_触摸[event.originalEvent.type].y=touch.pageY;
打破
“触摸移动”案例:
案例“mousemove”:
_触摸[event.originalEvent.type].x=touch.pageX;
_触摸[event.originalEvent.type].y=touch.pageY;
打破
“touchend”案例:
“鼠标”一案:
_触摸[event.originalEvent.type]=true;
如果(_touch.touchstart.x>-1&&u touch.touchmove.x>-1){
xSwipe=Math.abs(_-touchs.touchtstart.x-_-touchs.touchmove.x);
ySwipe=Math.abs(_-touchs.touchstart.y-_-touchs.touchsmove.y);
}
if(xSwipe>ySwipe&&xSwipe>(getViewport().width*.33)){
_touchs.direction=\u touchs.touchstart.x<\u touchs.touchmove.x?'left':'right';
如果(_.direction==='left'){
//做点什么
}如果(_.direction==='right'){
//做点什么
}
}
}
打破
}
}
}
}
$element.on('touchstart touchmove touchend',函数(e){
触摸处理器(e);
});