简体   繁体   English

如何防止Dojo滑动手势冒泡?

[英]How to prevent Dojo swipe gesture from bubbling?

I have a seemingly simple question, but I can't seem to figure out where I'm going wrong. 我有一个看似简单的问题,但似乎无法弄清楚我要去哪里。 My skills are admittedly lacking in Dojo, so please excuse my naivety. 道场(Dojo)缺乏我的技能,所以请原谅我。

What I'm trying to accomplish : I have an element in the DOM that I want to respond to swipe and click events. 我要完成的工作 :DOM中有一个元素,我想响应滑动单击事件。 However, when the swipe.end event is triggered, the click event is being fired also. 但是,当触发swipe.end事件时,也会触发click事件。 I've tried to prevent the bubbling of the event (I'm assuming that the event is bubbling in this case), by calling stopPropagation and outright calling event.stop on the event, to no avail. 我试图通过在事件上调用stopPropagation和直接调用event.stop来防止事件冒泡(我假设在这种情况下事件正在冒泡),但event.stop You can see a code snippet below, or check out a working fiddle . 您可以在下面查看代码段,或查看有效的小提琴

HTML 的HTML

<div id='testSwipe'>Swipe Me</div>​

JS JS

require({
}, [ 
    'dojo/dom', 
    'dojox/gesture/swipe',
    'dojo/on',
    'dojo/_base/event'
], function(dom, swipe, on, event) {
    var div = dom.byId('testSwipe');

    on(div, swipe.end, function(e) {
        console.log("### SWIPE");

        e.stopPropagation();  // Click event still fires
        event.stop(e);  // Click event STILL fires
    });

    on(div, 'click', function(e) {
        console.log("### CLICK");
    });
});

In this example, swiping the event will cause the following output: 在此示例中,轻扫事件将导致以下输出:

### SWIPE
### CLICK

Any suggestions? 有什么建议么?

The problem is two different events are being fired: an event (with event.type == 'swipe' ) for the swipe and a mouseEvent for the click. 问题是触发了两个不同的事件:一个event (带有event.type == 'swipe' )用于滑动,一个mouseEvent用于单击。 The events aren't bubbling. 这些事件没有冒泡。

One way of achieving what you want is to block the click event if the swipe.end event has just fired. 实现您想要的一种方法是,如果刚刚触发了swipe.end事件,则阻止click事件。 It feels like a bit of a hack, but works. 感觉有点像hack,但是可以用。 See updated JSFiddle . 参见更新的JSFiddle

As a side note: it seems the Dojo way to prevent bubbling is to use dojo.stopEvent(e) as per the docs . 附带说明:防止冒泡的Dojo方法似乎是根据docs使用dojo.stopEvent(e) I did try this, but as expected it had no effect. 我确实尝试过此方法,但正如预期的那样,它没有效果。

The click event will always fire when you click on the element. 当您单击元素时,click事件将始终触发。 Instead of using click event you can use tap gesture using dojox/gesture/tap 除了使用点击事件,您还可以使用dojox / gesture / tap使用点击手势

on(div, tap, function(e) {
    console.log("### CLICK");
});

updated the fiddle 更新了小提琴

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM