简体   繁体   English

防止子事件触发

[英]Prevent child event from firing

I have a slider that I am currently making. 我有一个正在制作的滑块。 I am making slow progress, but I am making progress nonetheless! 我的进度很慢,但我的进度还是快! Currently I have this: 目前我有这个:

http://codepen.io/r3plica/pen/mEKyGG?editors=1011#0 http://codepen.io/r3plica/pen/mEKyGG?editors=1011#0

There are 2 things you can do with this control, the first thing is you can drag left or right. 使用此控件可以做两件事,第一件事是可以向左或向右拖动。 The second thing you can do is click a "point" and it will scroll to the center. 您可以做的第二件事是单击“点”,它将滚动到中心。 The problem I have is that if I start dragging from a point, when I let go it will invoke the moveToCenter method. 我的问题是,如果我从某个点开始拖动,放开它就会调用moveToCenter方法。

I have tried to prevent this by adding 我试图通过添加来防止这种情况

// Stop from accessing any child events
e.preventDefault();
e.stopPropagation();

to the end of the dragEventHandler , but this did not work. dragEventHandler的末尾 ,但这没有用。 I also have 2 boolean values options.drag and options.start . 我也有2个布尔值options.dragoptions.start I though I might be able to use them somehow (if the drag has started and is enabled then don't perform the moveToCenter but this didn't work either. 我虽然可以或许以某种方式使用它们(如果拖动已开始并已启用,则不要执行moveToCenter,但这也不起作用。

Do anyone have any idea how to get this to work? 有谁知道如何使它起作用?

Maybe this will help. 也许这会有所帮助。 You can register your events in bubbling or capturing mode, using addEventListener method. 您可以使用addEventListener方法以冒泡或捕获模式注册事件。 It defines orders of processing your events - child -> parent (bubbling), or vice versa (capturing). 它定义了处理事件的顺序-子->父(冒泡),反之亦然(捕获)。

http://www.quirksmode.org/js/events_advanced.html http://www.quirksmode.org/js/events_advanced.html

So, if you use addEventListener(event, handler, true ), it will use capturing event mode. 因此,如果您使用addEventListener(event,handler, true ),它将使用捕获事件模式。

Codepen: Codepen:

http://codepen.io/anon/pen/bZKdqV?editors=1011 http://codepen.io/anon/pen/bZKdqV?editors=1011

divs.forEach(function (div) {
      div.addEventListener('click', function(e) {
        e.stopPropagation();
        console.log('parent');
      }, true);  
  });

Be aware of browser support (IE9+). 注意浏览器支持(IE9 +)。 All modern browsers - yes, of course. 所有现代浏览器-是的,当然可以。

http://caniuse.com/#search=addeventlistener http://caniuse.com/#search=addeventlistener

Update 更新资料

So it turned out to be easier than first approach. 因此,事实证明这比第一种方法容易。 (no need for capturing) Check out codepen: (无需捕获)签出代码笔:

http://codepen.io/anon/pen/QExjzV?editors=1010 http://codepen.io/anon/pen/QExjzV?editors=1010

Changes from your sample: At the beginning of moveToCenter: function(e, options, animate) function 对样本的更改:在moveToCenter的开头:function(e,options,animate) function

if (options.started) {
  return;
}

In if (['mouseup', 'mouseleave'].indexOf(e.type) > -1) : if([[mouseup','mouseleave']。indexOf(e.type)> -1)中

setTimeout(function() {
  options.started = false;
} , 100);

instead of 代替

options.started = false;

Hope this helps. 希望这可以帮助。

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

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