Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is capture mode on an event listener

Tags:

vue.js

In the vue docs under Event Modifiers, there's an example of a modifier called capture which states the following:

<!-- use capture mode when adding the event listener -->
<div v-on:click.capture="doThis">...</div>

I've done some searching, but haven't found a clear answer as to how this modifies the event binding, then I thought to myself 'You know who always has the answer? Stack Overflow'

like image 750
autoboxer Avatar asked Feb 02 '17 01:02

autoboxer


People also ask

What is event capture?

Event capturing is the event starts from top element to the target element. It is the opposite of Event bubbling, which starts from target element to the top element.

What is the difference between event bubbling and capturing?

Event Bubbling − Whenever an event happens on an element, the event handlers will first run on it and then on its parent and finally all the way up to its other ancestors. Event Capturing − It is the reverse of the event bubbling and here the event starts from the parent element and then to its child element.

What is capturing phase and bubbling phase?

With bubbling, the event is first captured and handled by the innermost element and then propagated to outer elements. With capturing, the event is first captured by the outermost element and propagated to the inner elements.

What is the use of event capturing in JavaScript?

Event capturing is one of two ways to do event propagation in the HTML DOM. In event capturing, an event propagates from the outermost element to the target element. It is the opposite of event bubbling, where events propagate outwards from the target to the outer elements. Capturing happens before bubbling.


2 Answers

So right after posting I stumbled on this article which illustrates it clearly. Let's say for this example that you have three elements nested within each other:

<div class="outer">     <div class="middle">         <div class="inner"></div>     </div> </div> 

When a click event occurs, there are two phases: the first is called capturing, the second is called bubbling. When you click on the .inner, the event traverses down from the outermost container element .outer, to .middle, then to .inner in the capturing phase, then from .inner to .middle, then to .outer in the bubbling phase.

If capture is set on .inner for a click event handler:

<div class="outer">     <div class="middle">         <div class="inner" v-on:click.capture="doThis"></div>     </div> </div> 

and you click on it, it will call doThis(...) three times, the first from .outer, the second from .middle, and the third from .inner.

If capture is set on .middle for a click event handler

<div class="outer">     <div class="middle" v-on:click.capture="doThis">         <div class="inner"></div>     </div> </div> 

and you click on it, it will call doThis(...) two times, the first from .outer and the second from .middle

like image 161
autoboxer Avatar answered Sep 18 '22 17:09

autoboxer


Both bubble and capture event handlers on an element will be triggered only once, the difference is when they are triggered (before the children or after the children). Capture mode means the handler is triggered before any handlers on child elements. Bubble mode (the default), means the handler is triggered after all child elements have finished their handlers. You can even put a capture mode and bubble mode event handler by doing <div v-on:click="doThis" v-on:click.capture="doThis">

This JS fiddle demonstrates how a single click to a nested inner element first triggers capture handlers in an "outer-to-inner" order, and then triggers bubble handlers in an "inner-to-outer" order.

like image 36
absolutelyNoWarranty Avatar answered Sep 21 '22 17:09

absolutelyNoWarranty