header.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Header enhancements for more intelligent dynamic headers.
  3. *
  4. * This sets the masthead to the height of an uploaded image and applies sticky navigation.
  5. */
  6. ( function( $ ) {
  7. // Fit header into the available space
  8. function fitHeader() {
  9. var windowWidth = $( window ).width();
  10. var headerHeight = $( '#pique-header-image' ).height();
  11. var navHeight = $( '#primary-menu' ).height();
  12. var brandingHeight = $( '.site-branding' ).height();
  13. // Make sure we're not on the homepage, since that handles stuff differently
  14. if ( ! $( 'body' ).hasClass( 'pique-frontpage' )) {
  15. if ( 0 < $( '#pique-header-image' ).length ) {
  16. // Add the height of our header image and the height of our nav
  17. $( '#masthead' ).css( 'height', headerHeight + navHeight );
  18. } else {
  19. // Give enough room for our branding
  20. console.log('no header');
  21. $( '#masthead' ).css( 'height', brandingHeight + navHeight + 40 );
  22. }
  23. }
  24. };
  25. // Priority+ navigation, whee!
  26. function priorityNav() {
  27. // Make sure we have a menu and that the more-more item is present
  28. if ( 0 < $( '#site-navigation' ).length && 0 < $( '#more-menu' ).length ) {
  29. var navWidth = 0;
  30. var firstMoreElement = $( '#more-menu li' ).first();
  31. // Calculate the width of our "more" containing element
  32. var moreWidth = $( '#more-menu' ).outerWidth( true );
  33. // Calculate the current width of our navigation
  34. $( '#site-navigation .menu > li' ).each( function() {
  35. navWidth += $( this ).outerWidth( true );
  36. });
  37. // Calculate our available space
  38. var availableSpace = $( '#site-navigation' ).outerWidth( true ) - moreWidth;
  39. // If our nav is wider than our available space, we're going to move items
  40. if (navWidth > availableSpace) {
  41. var lastItem = $( '#site-navigation .menu > li:not(#more-menu)' ).last();
  42. lastItem.attr( 'data-width', lastItem.outerWidth( true ) );
  43. lastItem.prependTo( $( '#more-menu .sub-menu' ).eq( 0 ) );
  44. priorityNav(); // Rerun this function!
  45. // But if we have the extra space, we should add the items back to our menu
  46. } else if (navWidth + firstMoreElement.data( 'width' ) < availableSpace) {
  47. // Check to be sure there's enough space for our extra element
  48. firstMoreElement.insertBefore( $( '#site-navigation .menu > li' ).last() );
  49. priorityNav();
  50. }
  51. // Hide our more-menu entirely if there's nothing in it
  52. if ($( '#more-menu li' ).length > 0) {
  53. $( '#more-menu' ).addClass( 'visible' );
  54. } else {
  55. $( '#more-menu' ).removeClass( 'visible' );
  56. }
  57. } // check for body class
  58. }; // function priorityNav
  59. // Okay, now we want to stick-ify our menu when we reach it
  60. function stickyNav() {
  61. var nav_container = $( '#site-navigation-wrapper' );
  62. var nav = $( '#site-navigation' );
  63. nav_container.waypoint( {
  64. handler: function(direction) {
  65. nav.toggleClass( 'sticky', direction == 'down' );
  66. // Ensure we don't have an awkward jump when the menu sticks
  67. if ('down' === direction) {
  68. nav_container.css( { 'height':nav.outerHeight() } );
  69. } else {
  70. nav_container.css( { 'height':'auto' } );
  71. }
  72. }
  73. });
  74. };
  75. // Run our functions once the window has loaded fully
  76. $( window ).on( 'load', function() {
  77. priorityNav();
  78. fitHeader();
  79. stickyNav();
  80. });
  81. // Annnnnd also every time the window resizes
  82. var isResizing = false;
  83. $( window ).on('resize', function() {
  84. if (isResizing) {
  85. return;
  86. }
  87. isResizing = true;
  88. setTimeout(function() {
  89. priorityNav();
  90. fitHeader();
  91. isResizing = false;
  92. }, 150);
  93. });
  94. } )( jQuery );