global.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ( function( $ ) {
  2. var $header = $( '.header-top:first' ),
  3. $headerHeight = $( '.header-top' ).innerHeight(),
  4. $headerOffset = $( '.custom-header' ).innerHeight(),
  5. $headerHiddenClass = 'site-header-hidden',
  6. $headerFixedClass = 'site-header-fixed',
  7. $navigation = $( '.main-navigation' ),
  8. $navMenuItem = $navigation.find( '.menu-item' ),
  9. navigationHeight,
  10. navigationOuterHeight,
  11. navPadding,
  12. navMenuItemHeight,
  13. idealNavHeight,
  14. navIsNotTooTall;
  15. // adjust header margin based on height of menu
  16. function adjustHeaderMargin(){
  17. // check to see if on mobile by checking menu-toggle display
  18. if ( 'none' === $( '.menu-toggle').css( 'display') ) {
  19. // Don't define this 'til we're using it, it may change
  20. $headerHeight = $( '.header-top' ).innerHeight();
  21. // if yes, we want to bump the custom header down a bit, so the menu doesn't cut it off
  22. $( '.custom-header-image').css( 'margin-top', $headerHeight );
  23. }
  24. }
  25. // Make sure the nav isn't taller than two rows
  26. navigationHeight = $navigation.height();
  27. navMenuItemHeight = $navMenuItem.outerHeight() * 3;
  28. idealNavHeight = navMenuItemHeight;
  29. navIsNotTooTall = navigationHeight <= idealNavHeight;
  30. //we add the scroll class to the navs
  31. function adjustScrollClass() {
  32. // Make sure we're not on a mobile screen
  33. if ( 'none' === $( '.menu-toggle').css( 'display') ) {
  34. if ( $( window ).scrollTop() <= $headerOffset && $header.hasClass( $headerFixedClass ) ) {
  35. $header.removeClass( $headerFixedClass );
  36. $header.addClass( $headerHiddenClass );
  37. } else if ( $( window ).scrollTop() >= $headerOffset ) { //If the scroll is more than the custom header
  38. // Make sure the menu is not too tall
  39. if ( navIsNotTooTall || navMenuItemHeight == 0 ) {
  40. // Don't define this 'til we're using it, it may change
  41. $headerHeight = $( '.header-top' ).innerHeight( true );
  42. // If not, fix that header!
  43. $header.addClass( $headerFixedClass );
  44. $header.removeClass( $headerHiddenClass );
  45. $( '.custom-header' ).css( 'margin-top', $headerHeight );
  46. }
  47. } else {
  48. //If not we remove it
  49. $header.removeClass( $headerFixedClass );
  50. $header.removeClass( $headerHiddenClass );
  51. $( '.custom-header' ).css( 'margin-top', 'auto' );
  52. }
  53. }
  54. }
  55. // Check to see if iOS device
  56. // iOS devices make a mess of background-attachment: fixed and background-size: cover
  57. function checkiOS() {
  58. return /iPad|iPhone|iPod/.test(navigator.userAgent) && ! window.MSStream;
  59. }
  60. /*
  61. * Test if background-attachment: fixed is supported.
  62. * @link http://stackoverflow.com/questions/14115080/detect-support-for-background-attachment-fixed
  63. */
  64. function supportsFixedBackground() {
  65. var el = document.createElement('div'),
  66. isSupported;
  67. try {
  68. if ( ! ( 'backgroundAttachment' in el.style ) || checkiOS() ) {
  69. return false;
  70. }
  71. el.style.backgroundAttachment = 'fixed';
  72. isSupported = ( 'fixed' === el.style.backgroundAttachment );
  73. return isSupported;
  74. }
  75. catch (e) {
  76. return false;
  77. }
  78. }
  79. // Let's fire some JavaScript!
  80. $( document ).ready( function() {
  81. // On load, we want to adjust the header margin
  82. adjustHeaderMargin();
  83. adjustScrollClass();
  84. // We also want to check the device, and add a class if not iOS:
  85. if ( false === supportsFixedBackground() ) {
  86. document.documentElement.className += ' no-background-fixed';
  87. }
  88. } );
  89. // On scroll, we want to stick/unstick the header
  90. $( window ).on( 'scroll', function() {
  91. adjustScrollClass();
  92. } );
  93. // we also want to do the same on window rezize
  94. $( window ).on( 'resize', function() {
  95. setTimeout( adjustHeaderMargin, 500 );
  96. } );
  97. // Use mutant observer to check when .wf-active is added to theme for Custom Fonts.
  98. // That way we can make sure the header is the correct height when it happens.
  99. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  100. $.fn.attrchange = function(callback) {
  101. if (MutationObserver) {
  102. var options = {
  103. subtree: false,
  104. attributes: true
  105. };
  106. var observer = new MutationObserver(function(mutations) {
  107. mutations.forEach(function(e) {
  108. callback.call(e.target, e.attributeName);
  109. });
  110. });
  111. return this.each(function() {
  112. observer.observe(this, options);
  113. });
  114. }
  115. }
  116. $( 'html' ).attrchange( function( attrName ) {
  117. if( attrName == 'class' && $( 'html').hasClass( 'wf-active' ) ) {
  118. adjustHeaderMargin();
  119. }
  120. } );
  121. } )( jQuery );