custom.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // The number of pixels the user must scroll by before the logo is hidden.
  2. const scrollTopPixels = 234;
  3. // The margin to apply to the menu when the search bar is made fixed.
  4. // Should roughly match the logo's height as to not hide the top menu items
  5. // behind it.
  6. const menuTopMargin = '330px';
  7. // Hide the navigation bar logo when scrolling down on desktop platforms.
  8. // The logo is quite tall, so this helps make the rest of the navigation bar
  9. // more readable.
  10. function registerOnScrollEvent(mediaQuery) {
  11. // The navigation bar that contains the logo.
  12. const $navbar = $('.wy-side-scroll');
  13. const $menu = $('.wy-menu-vertical');
  14. const $search = $('.wy-side-nav-search');
  15. // The anchor that contains the logo. This element will be hidden
  16. // (instead of hiding just the logo), otherwise, a small clickable area
  17. // would remain visible.
  18. const $logo = $('.wy-side-nav-search > a');
  19. if (mediaQuery.matches) {
  20. // We're on desktop; register the scroll event.
  21. $navbar.scroll(function() {
  22. if ($(this).scrollTop() >= scrollTopPixels) {
  23. $logo.hide();
  24. $search.css('position', 'fixed');
  25. $menu.css('margin-top', menuTopMargin);
  26. } else {
  27. $logo.show();
  28. $search.css('position', 'static');
  29. $menu.css('margin-top', 0);
  30. }
  31. });
  32. } else {
  33. // We're on mobile; unregister the scroll event so the logo isn't hidden
  34. // when scrolling.
  35. $logo.show();
  36. $navbar.unbind('scroll');
  37. }
  38. }
  39. $(document).ready(() => {
  40. const mediaQuery = window.matchMedia('only screen and (min-width: 768px)');
  41. registerOnScrollEvent(mediaQuery);
  42. mediaQuery.addListener(registerOnScrollEvent);
  43. });