From d84a9d224a4fdf056ecf76397b1711f19dba5afa Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Sun, 20 Dec 2020 17:13:08 -0700 Subject: [PATCH] refactor logic for navbar burger to align with other scripts --- src/js/05-mobile-navbar.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/js/05-mobile-navbar.js b/src/js/05-mobile-navbar.js index ba5cb09..17f0452 100644 --- a/src/js/05-mobile-navbar.js +++ b/src/js/05-mobile-navbar.js @@ -1,12 +1,14 @@ -document.addEventListener('DOMContentLoaded', function () { - var navbarToggles = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0) - if (navbarToggles.length === 0) return - navbarToggles.forEach(function (el) { - el.addEventListener('click', function (e) { - e.stopPropagation() - el.classList.toggle('is-active') - document.getElementById(el.dataset.target).classList.toggle('is-active') - document.documentElement.classList.toggle('is-clipped--navbar') - }) - }) -}) +;(function () { + 'use strict' + + var navbarBurger = document.querySelector('.navbar-burger') + if (!navbarBurger) return + navbarBurger.addEventListener('click', toggleNavbarMenu.bind(navbarBurger)) + + function toggleNavbarMenu (e) { + e.stopPropagation() // trap event + this.classList.toggle('is-active') + document.getElementById(this.dataset.target).classList.toggle('is-active') + document.documentElement.classList.toggle('is-clipped--navbar') + } +})() -- 2.11.0