common.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. $(document).ready(function() {
  2. // Check for click events on the navbar burger icon
  3. $(".navbar-burger").click(function() {
  4. // Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
  5. $(".navbar-burger").toggleClass("is-active");
  6. $(".navbar-menu").toggleClass("is-active");
  7. });
  8. // Get all dropdowns on the page that aren't hoverable.
  9. const dropdowns = document.querySelectorAll('.dropdown:not(.is-hoverable)');
  10. if (dropdowns.length > 0) {
  11. // For each dropdown, add event handler to open on click.
  12. dropdowns.forEach(function(el) {
  13. el.addEventListener('click', function(e) {
  14. e.stopPropagation();
  15. el.classList.toggle('is-active');
  16. });
  17. });
  18. // If user clicks outside dropdown, close it.
  19. document.addEventListener('click', function(e) {
  20. closeDropdowns();
  21. });
  22. }
  23. /*
  24. * Close dropdowns by removing `is-active` class.
  25. */
  26. function closeDropdowns() {
  27. dropdowns.forEach(function(el) {
  28. el.classList.remove('is-active');
  29. });
  30. }
  31. // Close dropdowns if ESC pressed
  32. document.addEventListener('keydown', function (event) {
  33. let e = event || window.event;
  34. if (e.key === 'Esc' || e.key === 'Escape') {
  35. closeDropdowns();
  36. }
  37. });
  38. });