- commit
- 48f82a0024019ed21589f3947c7b39b2fad81382
- parent
- dc4ea7695d2fba7abc01a304162bf2ccf6766273
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2017-09-16 09:33
use Element.closest
Diffstat
| M | smooth-scroll.js | 25 | +++++++++++++++++++------ |
1 files changed, 19 insertions, 6 deletions
diff --git a/smooth-scroll.js b/smooth-scroll.js
@@ -11,15 +11,28 @@ 11 11 })(function() { 12 12 'use strict'; 13 1314 -1 var closestMatch = function(el, selector, root) {15 -1 if (el && el !== root) {16 -1 return el.matches(selector) ? el : closestMatch(el.parentElement, selector, root);17 -1 }18 -1 };-1 14 Element.prototype.closest = -1 15 Element.prototype.closest || -1 16 function(selector) { -1 17 var matches = document.querySelectorAll(selector); -1 18 -1 19 var fn = function(el) { -1 20 if (el) { -1 21 for (var i = 0; i < matches.length; i++) { -1 22 if (matches[i] === el) { -1 23 return el; -1 24 } -1 25 } -1 26 return fn(el.parentElement); -1 27 } -1 28 }; -1 29 -1 30 return fn(this); -1 31 }; 19 32 20 33 var delegated = function(element, eventType, selector, handler) { 21 34 element.addEventListener(eventType, function(event) {22 -1 var target = closestMatch(event.target, selector, element);-1 35 var target = event.target.closest(selector); 23 36 if (target) { 24 37 handler(event, target); 25 38 }