我想使用滑动来实现触摸设备上的导航。我尝试了jGestures库,但它不适合滑动操作。 我已被建议不要使用jquery mobile。
是否有其他方法可以实现滑动?
编辑: 它不能清晰地检测到滑动。我在多个设备上进行了测试,包括iPad,在我的情况下需要多次滑动才能完成一个操作(路由)。
我为自己的需求编写了这个函数。
欢迎使用。在移动设备上表现很棒。
function detectswipe(el,func) {
swipe_det = new Object();
swipe_det.sX = 0; swipe_det.sY = 0; swipe_det.eX = 0; swipe_det.eY = 0;
var min_x = 30; //min x swipe for horizontal swipe
var max_x = 30; //max x difference for vertical swipe
var min_y = 50; //min y swipe for vertical swipe
var max_y = 60; //max y difference for horizontal swipe
var direc = "";
ele = document.getElementById(el);
ele.addEventListener('touchstart',function(e){
var t = e.touches[0];
swipe_det.sX = t.screenX;
swipe_det.sY = t.screenY;
},false);
ele.addEventListener('touchmove',function(e){
e.preventDefault();
var t = e.touches[0];
swipe_det.eX = t.screenX;
swipe_det.eY = t.screenY;
},false);
ele.addEventListener('touchend',function(e){
//horizontal detection
if ((((swipe_det.eX - min_x > swipe_det.sX) || (swipe_det.eX + min_x < swipe_det.sX)) && ((swipe_det.eY < swipe_det.sY + max_y) && (swipe_det.sY > swipe_det.eY - max_y) && (swipe_det.eX > 0)))) {
if(swipe_det.eX > swipe_det.sX) direc = "r";
else direc = "l";
}
//vertical detection
else if ((((swipe_det.eY - min_y > swipe_det.sY) || (swipe_det.eY + min_y < swipe_det.sY)) && ((swipe_det.eX < swipe_det.sX + max_x) && (swipe_det.sX > swipe_det.eX - max_x) && (swipe_det.eY > 0)))) {
if(swipe_det.eY > swipe_det.sY) direc = "d";
else direc = "u";
}
if (direc != "") {
if(typeof func == 'function') func(el,direc);
}
direc = "";
swipe_det.sX = 0; swipe_det.sY = 0; swipe_det.eX = 0; swipe_det.eY = 0;
},false);
}
function myfunction(el,d) {
alert("you swiped on element with id '"+el+"' to "+d+" direction");
}
要使用这个函数,只需像这样使用它
detectswipe('an_element_id',myfunction);
detectswipe('an_other_element_id',my_other_function);
min_x
:min_x = Math.abs(swipe_det.eY - swipe_det.sY)
。其他方向同理。因为当你向上滑动400像素,只向右滑动40像素时,你可能想要向上滑动。然而,你的代码会识别为向右滑动。 - Sadıkabs((eX - sX) / (eY - sY))
),选择垂直和水平会更精确。您可以在这里看到我的分支版本。如果这条评论得到足够的点赞(比如说 5 个),我将编辑回答。 - Ulysse BN你尝试过Hammerjs吗?它利用触摸的速度支持滑动手势。 http://eightmedia.github.com/hammer.js/
注意:受到EscapeNetscape的回答的启发,我使用现代JavaScript编辑了他的脚本,并在评论中进行了修改。由于用户的兴趣和4小时的jsfiddle.net宕机,我将此问题的答案公布出来。在您使用之前,建议先阅读一下这个detectSwipe
函数。如果需要,请随意查看并编辑该答案。
这里有一个detectSwipe
函数,工作得非常好(在我的一个网站上使用过)。建议在使用之前先阅读一下它。如有需要,可以自由审核并编辑答案。
// usage example
detectSwipe('swipeme', (el, dir) => alert(`you swiped on element with id ${el.id} to ${dir} direction`))
// source code
// Tune deltaMin according to your needs. Near 0 it will almost
// always trigger, with a big value it can never trigger.
function detectSwipe(id, func, deltaMin = 90) {
const swipe_det = {
sX: 0,
sY: 0,
eX: 0,
eY: 0
}
// Directions enumeration
const directions = Object.freeze({
UP: 'up',
DOWN: 'down',
RIGHT: 'right',
LEFT: 'left'
})
let direction = null
const el = document.getElementById(id)
el.addEventListener('touchstart', function(e) {
const t = e.touches[0]
swipe_det.sX = t.screenX
swipe_det.sY = t.screenY
}, false)
el.addEventListener('touchmove', function(e) {
// Prevent default will stop user from scrolling, use with care
// e.preventDefault();
const t = e.touches[0]
swipe_det.eX = t.screenX
swipe_det.eY = t.screenY
}, false)
el.addEventListener('touchend', function(e) {
const deltaX = swipe_det.eX - swipe_det.sX
const deltaY = swipe_det.eY - swipe_det.sY
// Min swipe distance, you could use absolute value rather
// than square. It just felt better for personnal use
if (deltaX ** 2 + deltaY ** 2 < deltaMin ** 2) return
// horizontal
if (deltaY === 0 || Math.abs(deltaX / deltaY) > 1)
direction = deltaX > 0 ? directions.RIGHT : directions.LEFT
else // vertical
direction = deltaY > 0 ? directions.UP : directions.DOWN
if (direction && typeof func === 'function') func(el, direction)
direction = null
}, false)
}
#swipeme {
width: 100%;
height: 100%;
background-color: orange;
color: black;
text-align: center;
padding-top: 20%;
padding-bottom: 20%;
}
<div id='swipeme'>
swipe me
</div>
虽然我有些自吹自擂,但你可能需要考虑一个我写的jQuery插件:
https://github.com/benmajor/jQuery-Mobile-Events
它不需要使用jQuery Mobile,只需要jQuery即可。
我喜欢你的解决方案并在我的网站上实现了它 - 然而,进行了一些小改进。只是想分享一下我的代码:
function detectSwipe(id, f) {
var detect = {
startX: 0,
startY: 0,
endX: 0,
endY: 0,
minX: 30, // min X swipe for horizontal swipe
maxX: 30, // max X difference for vertical swipe
minY: 50, // min Y swipe for vertial swipe
maxY: 60 // max Y difference for horizontal swipe
},
direction = null,
element = document.getElementById(id);
element.addEventListener('touchstart', function (event) {
var touch = event.touches[0];
detect.startX = touch.screenX;
detect.startY = touch.screenY;
});
element.addEventListener('touchmove', function (event) {
event.preventDefault();
var touch = event.touches[0];
detect.endX = touch.screenX;
detect.endY = touch.screenY;
});
element.addEventListener('touchend', function (event) {
if (
// Horizontal move.
(Math.abs(detect.endX - detect.startX) > detect.minX)
&& (Math.abs(detect.endY - detect.startY) < detect.maxY)
) {
direction = (detect.endX > detect.startX) ? 'right' : 'left';
} else if (
// Vertical move.
(Math.abs(detect.endY - detect.startY) > detect.minY)
&& (Math.abs(detect.endX - detect.startX) < detect.maxX)
) {
direction = (detect.endY > detect.startY) ? 'down' : 'up';
}
if ((direction !== null) && (typeof f === 'function')) {
f(element, direction);
}
});
}
使用方式如下:
detectSwipe('an_element_id', myfunction);
或者
detectSwipe('another_element_id', my_other_function);
myfunction
,并带有参数element-id和'left'
、'right'
、'up'
或'down'
。锤子时间!
我使用了Hammer JS,并且它能够与手势一起工作。从这里阅读详细信息:https://hammerjs.github.io/
好消息是,它比jQuery mobile更轻量级和快速。您也可以在他们的网站上进行测试。
我看了几个解决方案,但都因为滚动和选择文本而失败,这是最大的困惑。我不是向右滚动,而是关闭框等。
我刚刚完成了我的实现,它可以为我完成所有操作。
https://github.com/webdevelopers-eu/jquery-dna-gestures
这是MIT许可,所以你可以随意使用 - 而且它确实非常简单 - 经过压缩只有800字节。你可以在我的(正在开发中的)网站https://cyrex.tech上查看 - 在触摸设备上向右滑动应该可以关闭弹出窗口。