简体   繁体   English

当用户移动手指以滑动元素时如何防止触发“触摸”事件

[英]How to prevent 'touched' event from firing when user moves finger to slide the element

Solution : https://github.com/alexgibson/tap.js 解决方案https : //github.com/alexgibson/tap.js

I have a conflict between 'touchend' and 'touchmove' events on the iPad in mobile Safari. 我在移动Safari的iPad上的“ touchend”和“ touchmove”事件之间有冲突。 I have images sitting next to each other like a gallery and they have a 'touchend' event attached to flip when touchend. 我有像画廊一样挨着坐着的图像,它们有一个'touchend'事件附加到touchend翻转时。 However, you can also slide from one image to the other (like on iPhone sliding home screen to the next screen). 但是,您也可以从一个图像滑动到另一图像(例如在iPhone的主屏幕上滑动到下一屏幕)。

Now I can't figure out how to prevent the 'touchend' event from firing when I want to slide to the next image. 现在,当我想滑动到下一张图像时,我不知道如何防止触发“ touchend”事件。 Obviously, I don't want the image to flip if I slide, only if I tap. 显然,我只希望在滑动时才滑动图像。

My solution so far: 到目前为止我的解决方案:

var img = $('.show-video');

var sliding = false;

img.bind('touchend', function(e) {
    if (sliding === false){
        Animate($(this), 'flip');
    }
});

img.bind('touchmove', function(){
    sliding = true;
    $(this).bind('touchend', function(){
        window.setTimeout(function(){
            sliding = false;
        }, 200)
    })
});

```` ````

I think this can be done much better. 我认为可以做得更好。

You basically have two events when it comes to tapping the screen: touchstart and touchend . 轻触屏幕时,您基本上有两个事件: touchstarttouchend When a user touches the screen, the first event is captured, when he stops touching the screen, the second event is fired. 当用户触摸屏幕时,将捕获第一个事件,当用户停止触摸屏幕时,将触发第二个事件。 The touchmove event should be captured to analyse the movement of the finger. 应该捕获touchmove事件以分析手指的运动。

I'm not entirely sure what you're trying to say, but I'm assuming you you are capturing those events. 我不确定您要说什么,但我假设您正在捕获这些事件。 What you can do is: 您可以做的是:

  1. Instead of doing something on touchstart do it on touchend . 相反,在做某件事touchstart做它touchend This is more natural anyway, since something should happen after you lift the finger 无论如何这都是更自然的,因为在您举起手指后应该会发生一些事情
  2. After touchstart see if there is any movement using touchmove . 之后touchstart看是否有使用任何运动touchmove If none and touchend is called, do what you wanted to do for a tap. 如果没有调用touchend ,则执行您想做的操作。 If there was movement, do whatever you wanted to do for sliding. 如果有运动,请做任何您想做的滑动。

I hope this helps. 我希望这有帮助。

I've made a workaround on this once. 我曾经对此进行过解决。

You been already explained what are the events that are happening, and also you have a clue as a question. 已经为您解释了正在发生的事件,并且您也有疑问的线索。

For example lets say you have your gallery on a #galleryOverlay, and you scroller is inside it. 例如,假设您将画廊放在#galleryOverlay上,并且滚动条位于其中。

Then all you have to do is something like 然后,您要做的就是

$('#galleryOverlay').bind('touchstart touchmove touchmove', function(e){e.stopPropagation();}) $('#galleryOverlay')。bind('touchstart touchmove touchmove',function(e){e.stopPropagation();})

So this way you keep your events inside your gallery, but you prevent them to happen outside. 因此,您可以将事件保留在画廊中,但可以防止事件在画廊外部发生。

Good luck 祝好运

This library adds a 'tap' event which you can use to watch for a "tap" instead of "touchend". 该库添加了一个“ tap”事件,您可以使用它来监视“ tap”而不是“ touchend”。

https://github.com/alexgibson/tap.js https://github.com/alexgibson/tap.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户触摸并移动手指时,如何获取使用敲出.js触摸了哪些元素(虚拟机) - When user touches and moves his finger, how to get which elements (vms) were touched with knockout.js 当手指移动一点时,点击事件不会在触摸屏上触发 - Click event not firing on touchscreen when finger moves a bit 如何检测多点触摸手指何时移到子元素上? - How to detect when multitouch finger moves onto child element? 如果焦点移到一个特定元素上,如何防止我的聚焦功能触发? - How can I prevent my focusout function from firing if focus moves to one particular element? jQuery:在用户的手指触摸元素时防止页面滚动 - jQuery: Prevent page scroll when user's finger touches an element 在更改事件中隐藏元素时如何阻止事件触发 - How to stop event from firing when hiding element in a change event 如何在用户刷新页面时阻止函数触发? - How to prevent a function from firing off when the user refreshes a page? 当我呼叫“ <element> 。点击()”? 还是解决这个问题的更好方法? - How can I prevent a click event from firing when I call “<element>.click()”? Or what's a better way to solve this issue? 如何防止排队的事件回调触发? - How to prevent queued event callbacks from firing? 如何防止子事件在JQuery中触发 - How to prevent child event from firing in JQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM