auto-hide-nav.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * Auto-Hide Navigation Script - jQuery Version
  3. * Smoothly hides navigation when scrolling down and shows it when scrolling up
  4. */
  5. (function($) {
  6. 'use strict';
  7. // Configuration
  8. const config = {
  9. scrollThreshold: 100, // Start hiding after this many pixels
  10. mouseRevealZone: 100, // Show header when mouse is within this many pixels from top
  11. animationDuration: 300, // Animation duration in ms
  12. debounceDelay: 10 // Scroll event debounce delay
  13. };
  14. // State management
  15. let state = {
  16. lastScrollTop: 0,
  17. isVisible: true,
  18. headerHeight: 0,
  19. ticking: false
  20. };
  21. // DOM elements
  22. let $header, $body;
  23. // Initialize when document is ready
  24. $(document).ready(function() {
  25. init();
  26. });
  27. function init() {
  28. // console.log('Auto-hide navigation: jQuery script loaded');
  29. // Find header elements - Try multiple selectors for Twenty Twenty-Five
  30. $header = $('header').first();
  31. if (!$header.length) {
  32. $header = $('.wp-site-header').first();
  33. }
  34. if (!$header.length) {
  35. $header = $('.wp-block-template-part[data-area="header"]').first();
  36. }
  37. if (!$header.length) {
  38. $header = $('[class*="header"]').first();
  39. }
  40. if (!$header.length) {
  41. $header = $('nav').first();
  42. }
  43. $body = $('body');
  44. // console.log('Header element found:', $header[0]);
  45. // console.log('Header length:', $header.length);
  46. if (!$header.length) {
  47. console.warn('Auto-hide navigation: No header element found');
  48. return;
  49. }
  50. // Add identifying class for our CSS
  51. $header.addClass('srh-auto-hide-header');
  52. // Setup
  53. calculateHeaderHeight();
  54. bindEvents();
  55. // Initial state
  56. updateHeaderState();
  57. // console.log('Auto-hide navigation: Initialization complete');
  58. // console.log('Header height calculated:', state.headerHeight);
  59. }
  60. function calculateHeaderHeight() {
  61. if (!$header.length) return;
  62. state.headerHeight = $header.outerHeight() || 80;
  63. $('html').css('--header-height', state.headerHeight + 'px');
  64. // console.log('Header height:', state.headerHeight);
  65. }
  66. function bindEvents() {
  67. // Scroll event with debouncing
  68. $(window).on('scroll', debounce(handleScroll, config.debounceDelay));
  69. // Resize event
  70. $(window).on('resize', debounce(calculateHeaderHeight, 100));
  71. // Mouse movement for revealing header
  72. $(document).on('mousemove', handleMouseMove);
  73. // Keyboard events for accessibility
  74. $(document).on('keydown', handleKeydown);
  75. // Focus events for accessibility
  76. $header.find('a, button, input, textarea, select, [tabindex]').on('focus', showHeader);
  77. // Smooth scroll for anchor links
  78. $(document).on('click', 'a[href^="#"]', handleAnchorClick);
  79. // console.log('Events bound successfully');
  80. }
  81. function handleScroll() {
  82. if (!state.ticking) {
  83. requestAnimationFrame(updateHeaderState);
  84. state.ticking = true;
  85. }
  86. }
  87. function updateHeaderState() {
  88. const scrollTop = $(window).scrollTop();
  89. // Add scrolled class for styling
  90. if (scrollTop > 50) {
  91. $body.addClass('header-offset');
  92. $header.addClass('header-scrolled');
  93. } else {
  94. $body.removeClass('header-offset');
  95. $header.removeClass('header-scrolled');
  96. }
  97. // Hide/show logic
  98. if (scrollTop > config.scrollThreshold) {
  99. if (scrollTop > state.lastScrollTop && state.isVisible) {
  100. // Scrolling down - hide header
  101. hideHeader();
  102. } else if (scrollTop < state.lastScrollTop && !state.isVisible) {
  103. // Scrolling up - show header
  104. showHeader();
  105. }
  106. } else if (!state.isVisible) {
  107. // Near top - always show header
  108. showHeader();
  109. }
  110. state.lastScrollTop = scrollTop;
  111. state.ticking = false;
  112. }
  113. function hideHeader() {
  114. if (!state.isVisible) return;
  115. // console.log('Hiding header');
  116. state.isVisible = false;
  117. $header.removeClass('header-visible').addClass('header-hidden');
  118. // Dispatch custom event
  119. $(document).trigger('headerHidden', {
  120. headerElement: $header[0],
  121. isVisible: state.isVisible,
  122. scrollTop: state.lastScrollTop
  123. });
  124. }
  125. function showHeader() {
  126. if (state.isVisible) return;
  127. // console.log('Showing header');
  128. state.isVisible = true;
  129. $header.removeClass('header-hidden').addClass('header-visible');
  130. // Dispatch custom event
  131. $(document).trigger('headerVisible', {
  132. headerElement: $header[0],
  133. isVisible: state.isVisible,
  134. scrollTop: state.lastScrollTop
  135. });
  136. }
  137. function handleMouseMove(e) {
  138. if (e.clientY < config.mouseRevealZone &&
  139. $(window).scrollTop() > config.scrollThreshold &&
  140. !state.isVisible) {
  141. showHeader();
  142. }
  143. }
  144. function handleKeydown(e) {
  145. // Show header on Tab or Escape for accessibility
  146. if (e.key === 'Tab' || e.key === 'Escape') {
  147. showHeader();
  148. }
  149. }
  150. function handleAnchorClick(e) {
  151. const $link = $(e.currentTarget);
  152. const targetId = $link.attr('href').substring(1);
  153. const $target = $('#' + targetId);
  154. if ($target.length) {
  155. e.preventDefault();
  156. const offsetTop = $target.offset().top - state.headerHeight;
  157. $('html, body').animate({
  158. scrollTop: offsetTop
  159. }, 600);
  160. }
  161. }
  162. function debounce(func, wait) {
  163. let timeout;
  164. return function executedFunction() {
  165. const context = this;
  166. const args = arguments;
  167. const later = function() {
  168. clearTimeout(timeout);
  169. func.apply(context, args);
  170. };
  171. clearTimeout(timeout);
  172. timeout = setTimeout(later, wait);
  173. };
  174. }
  175. // Public API
  176. window.SRHNavigation = {
  177. show: showHeader,
  178. hide: hideHeader,
  179. toggle: function() {
  180. state.isVisible ? hideHeader() : showHeader();
  181. },
  182. getState: function() {
  183. return $.extend({}, state);
  184. },
  185. updateConfig: function(newConfig) {
  186. $.extend(config, newConfig);
  187. },
  188. getHeader: function() {
  189. return $header;
  190. }
  191. };
  192. })(jQuery);