menu.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Slide up/down
  3. * Code from https://dev.to/bmsvieira/vanilla-js-slidedown-up-4dkn
  4. * @param target
  5. * @param duration
  6. */
  7. let slideUp = (target: HTMLElement, duration = 500) => {
  8. target.classList.add('transiting');
  9. target.style.transitionProperty = 'height, margin, padding';
  10. target.style.transitionDuration = duration + 'ms';
  11. ///target.style.boxSizing = 'border-box';
  12. target.style.height = target.offsetHeight + 'px';
  13. target.offsetHeight;
  14. target.style.overflow = 'hidden';
  15. target.style.height = "0";
  16. target.style.paddingTop = "0";
  17. target.style.paddingBottom = "0";
  18. target.style.marginTop = "0";
  19. target.style.marginBottom = "0";
  20. window.setTimeout(() => {
  21. target.classList.remove('show')
  22. target.style.removeProperty('height');
  23. target.style.removeProperty('padding-top');
  24. target.style.removeProperty('padding-bottom');
  25. target.style.removeProperty('margin-top');
  26. target.style.removeProperty('margin-bottom');
  27. target.style.removeProperty('overflow');
  28. target.style.removeProperty('transition-duration');
  29. target.style.removeProperty('transition-property');
  30. target.classList.remove('transiting');
  31. }, duration);
  32. }
  33. let slideDown = (target: HTMLElement, duration = 500) => {
  34. target.classList.add('transiting');
  35. target.style.removeProperty('display');
  36. target.classList.add('show');
  37. let height = target.offsetHeight;
  38. target.style.overflow = 'hidden';
  39. target.style.height = "0";
  40. target.style.paddingTop = "0";
  41. target.style.paddingBottom = "0";
  42. target.style.marginTop = "0";
  43. target.style.marginBottom = "0";
  44. target.offsetHeight;
  45. ///target.style.boxSizing = 'border-box';
  46. target.style.transitionProperty = "height, margin, padding";
  47. target.style.transitionDuration = duration + 'ms';
  48. target.style.height = height + 'px';
  49. target.style.removeProperty('padding-top');
  50. target.style.removeProperty('padding-bottom');
  51. target.style.removeProperty('margin-top');
  52. target.style.removeProperty('margin-bottom');
  53. window.setTimeout(() => {
  54. target.style.removeProperty('height');
  55. target.style.removeProperty('overflow');
  56. target.style.removeProperty('transition-duration');
  57. target.style.removeProperty('transition-property');
  58. target.classList.remove('transiting');
  59. }, duration);
  60. }
  61. let slideToggle = (target, duration = 500) => {
  62. if (window.getComputedStyle(target).display === 'none') {
  63. return slideDown(target, duration);
  64. } else {
  65. return slideUp(target, duration);
  66. }
  67. }
  68. export default function () {
  69. const toggleMenu = document.getElementById('toggle-menu');
  70. if (toggleMenu) {
  71. toggleMenu.addEventListener('click', () => {
  72. if (document.getElementById('main-menu').classList.contains('transiting')) return;
  73. document.body.classList.toggle('show-menu');
  74. slideToggle(document.getElementById('main-menu'), 300);
  75. toggleMenu.classList.toggle('is-active');
  76. });
  77. }
  78. }