-
-
Save SergSlon/7524823 to your computer and use it in GitHub Desktop.
Event bubbling demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| * { | |
| box-sizing: border-box; | |
| margin: 0; | |
| padding: 20px; | |
| transition: background 800ms; | |
| } | |
| html { | |
| height: 100%; | |
| background: hsl(193, 66%, 55%); | |
| font: bold 40px helvetica, sans-serif; | |
| color: red; | |
| } | |
| body { | |
| height: 100%; | |
| background: hsl(193, 66%, 65%); | |
| } | |
| div { | |
| height: 100%; | |
| background: hsl(193, 66%, 75%); | |
| } | |
| ul { | |
| height: 100%; | |
| list-style: none; | |
| background: hsl(193, 66%, 85%); | |
| } | |
| li { | |
| height: 100%; | |
| background: hsl(193, 66%, 95%); | |
| } | |
| .highlight { | |
| background: red; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset=utf-8 /> | |
| <title>JS Bin</title> | |
| </head> | |
| <body> | |
| <div class=""> | |
| <ul> | |
| <li>Click on a layer to watch the event move through the DOM tree.</li> | |
| </ul> | |
| </div> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var html = document.documentElement; | |
| var body = document.body; | |
| var div = body.querySelector('div'); | |
| var ul = body.querySelector('ul'); | |
| var li = body.querySelector('li'); | |
| var pause = 200; | |
| // Capture | |
| html.addEventListener('click', callback, true); | |
| body.addEventListener('click', callback, true); | |
| div.addEventListener('click', callback, true); | |
| html.addEventListener('click', callback, true); | |
| ul.addEventListener('click', callback, true); | |
| li.addEventListener('click', callback, true); | |
| // Bubble | |
| html.addEventListener('click', callback, false); | |
| body.addEventListener('click', callback, false); | |
| div.addEventListener('click', callback, false); | |
| html.addEventListener('click', callback, false); | |
| ul.addEventListener('click', callback, false); | |
| li.addEventListener('click', callback, false); | |
| function callback(event) { | |
| var ms = event.timeout = (event.timeout + pause) || 0; | |
| var target = event.currentTarget; | |
| setTimeout(function() { | |
| target.classList.add('highlight'); | |
| setTimeout(function() { | |
| target.classList.remove('highlight'); | |
| }, pause); | |
| }, ms); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment