global.js 4.4 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 (
  35. $(window).scrollTop() <= $headerOffset &&
  36. $header.hasClass($headerFixedClass)
  37. ) {
  38. // accounts for empty or removed custom header image
  39. if (0 !== $headerOffset) {
  40. $header.removeClass($headerFixedClass);
  41. $header.addClass($headerHiddenClass);
  42. }
  43. } else if ($(window).scrollTop() >= $headerOffset) {
  44. //If the scroll is more than the custom header
  45. // Make sure the menu is not too tall
  46. if (navIsNotTooTall || navMenuItemHeight == 0) {
  47. // Don't define this 'til we're using it, it may change
  48. $headerHeight = $('.header-top').innerHeight(true);
  49. // If not, fix that header!
  50. $header.addClass($headerFixedClass);
  51. $header.removeClass($headerHiddenClass);
  52. $('.custom-header').css('margin-top', $headerHeight);
  53. }
  54. } else {
  55. //If not we remove it
  56. $header.removeClass($headerFixedClass);
  57. $header.removeClass($headerHiddenClass);
  58. $('.custom-header').css('margin-top', 'auto');
  59. }
  60. }
  61. }
  62. // Check to see if iOS device
  63. // iOS devices make a mess of background-attachment: fixed and background-size: cover
  64. function checkiOS() {
  65. return /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
  66. }
  67. /*
  68. * Test if background-attachment: fixed is supported.
  69. * @link http://stackoverflow.com/questions/14115080/detect-support-for-background-attachment-fixed
  70. */
  71. function supportsFixedBackground() {
  72. var el = document.createElement('div'),
  73. isSupported;
  74. try {
  75. if (!('backgroundAttachment' in el.style) || checkiOS()) {
  76. return false;
  77. }
  78. el.style.backgroundAttachment = 'fixed';
  79. isSupported = 'fixed' === el.style.backgroundAttachment;
  80. return isSupported;
  81. } catch (e) {
  82. return false;
  83. }
  84. }
  85. // Let's fire some JavaScript!
  86. $(document).ready(function () {
  87. // On load, we want to adjust the header margin
  88. adjustHeaderMargin();
  89. adjustScrollClass();
  90. // We also want to check the device, and add a class if not iOS:
  91. if (false === supportsFixedBackground()) {
  92. document.documentElement.className += ' no-background-fixed';
  93. }
  94. });
  95. // On scroll, we want to stick/unstick the header
  96. $(window).on('scroll', function () {
  97. adjustScrollClass();
  98. });
  99. // we also want to do the same on window rezize
  100. $(window).on('resize', function () {
  101. setTimeout(adjustHeaderMargin, 500);
  102. });
  103. // Use mutant observer to check when .wf-active is added to theme for Custom Fonts.
  104. // That way we can make sure the header is the correct height when it happens.
  105. var MutationObserver =
  106. window.MutationObserver ||
  107. window.WebKitMutationObserver ||
  108. window.MozMutationObserver;
  109. $.fn.attrchange = function (callback) {
  110. if (MutationObserver) {
  111. var options = {
  112. subtree: false,
  113. attributes: true,
  114. };
  115. var observer = new MutationObserver(function (mutations) {
  116. mutations.forEach(function (e) {
  117. callback.call(e.target, e.attributeName);
  118. });
  119. });
  120. return this.each(function () {
  121. observer.observe(this, options);
  122. });
  123. }
  124. };
  125. $('html').attrchange(function (attrName) {
  126. if (attrName == 'class' && $('html').hasClass('wf-active')) {
  127. adjustHeaderMargin();
  128. }
  129. });
  130. })(jQuery);