Advertisement
Guest User

(child theme) global.js

a guest
Mar 8th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* global twentyseventeenScreenReaderText */
  2. (function( $ ) {
  3.  
  4.     // Variables and DOM Caching.
  5.     var $body = $( 'body' ),
  6.         $customHeader = $body.find( '.custom-header' ),
  7.         $branding = $customHeader.find( '.site-branding' ),
  8.         $navigation = $body.find( '.navigation-top' ),
  9.         $navWrap = $navigation.find( '.wrap' ),
  10.         $navMenuItem = $navigation.find( '.menu-item' ),
  11.         $menuToggle = $navigation.find( '.menu-toggle' ),
  12.         $menuScrollDown = $body.find( '.menu-scroll-down' ),
  13.         $sidebar = $body.find( '#secondary' ),
  14.         $entryContent = $body.find( '.entry-content' ),
  15.         $formatQuote = $body.find( '.format-quote blockquote' ),
  16.         isFrontPage = $body.hasClass( 'twentyseventeen-front-page' ) || $body.hasClass( 'home blog' ),
  17.         navigationFixedClass = 'site-navigation-fixed',
  18.         navigationHeight,
  19.         navigationOuterHeight,
  20.         navPadding,
  21.         navMenuItemHeight,
  22.         idealNavHeight,
  23.         navIsNotTooTall,
  24.         headerOffset,
  25.         menuTop = 0,
  26.         resizeTimer;
  27.  
  28.     // Ensure the sticky navigation doesn't cover current focused links.
  29.     $( 'a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, [tabindex], [contenteditable]', '.site-content-contain' ).filter( ':visible' ).focus( function() {
  30.         if ( $navigation.hasClass( 'site-navigation-fixed' ) ) {
  31.             var windowScrollTop = $( window ).scrollTop(),
  32.                 fixedNavHeight = $navigation.height(),
  33.                 itemScrollTop = $( this ).offset().top,
  34.                 offsetDiff = itemScrollTop - windowScrollTop;
  35.  
  36.             // Account for Admin bar.
  37.             if ( $( '#wpadminbar' ).length ) {
  38.                 offsetDiff -= $( '#wpadminbar' ).height();
  39.             }
  40.  
  41.             if ( offsetDiff < fixedNavHeight ) {
  42.                 $( window ).scrollTo( itemScrollTop - ( fixedNavHeight + 50 ), 0 );
  43.             }
  44.         }
  45.     });
  46.  
  47.     // Set properties of navigation.
  48.     function setNavProps() {
  49.         navigationHeight      = $navigation.height();
  50.         navigationOuterHeight = $navigation.outerHeight();
  51.         navPadding            = parseFloat( $navWrap.css( 'padding-top' ) ) * 2;
  52.         navMenuItemHeight     = $navMenuItem.outerHeight() * 2;
  53.         idealNavHeight        = navPadding + navMenuItemHeight;
  54.         navIsNotTooTall       = navigationHeight <= idealNavHeight;
  55.     }
  56.  
  57.     // Make navigation 'stick'.
  58.     function adjustScrollClass() {
  59.  
  60.         // Make sure we're not on a mobile screen.
  61.         if ( 'none' === $menuToggle.css( 'display' ) ) {
  62.  
  63.             // Make sure the nav isn't taller than two rows.
  64.             if ( navIsNotTooTall ) {
  65.  
  66.                 headerOffset = $customHeader.innerHeight();
  67.  
  68.                 // If the scroll is more than the custom header, set the fixed class.
  69.                 if ( $( window ).scrollTop() >= headerOffset ) {
  70.                     $navigation.addClass( navigationFixedClass );
  71.                 } else {
  72.                     $navigation.removeClass( navigationFixedClass );
  73.                 }
  74.  
  75.             } else {
  76.  
  77.                 // Remove 'fixed' class if nav is taller than two rows.
  78.                 $navigation.removeClass( navigationFixedClass );
  79.             }
  80.         }
  81.     }
  82.  
  83.     // Set margins of branding in header.
  84.     function adjustHeaderHeight() {
  85.         if ( 'none' === $menuToggle.css( 'display' ) ) {
  86.  
  87.             // The margin should be applied to different elements on front-page or home vs interior pages.
  88.             if ( isFrontPage ) {
  89.                 $branding.css( 'margin-bottom', navigationOuterHeight );
  90.             } else {
  91.                 $customHeader.css( 'margin-bottom', navigationOuterHeight );
  92.             }
  93.  
  94.         } else {
  95.             $customHeader.css( 'margin-bottom', '0' );
  96.             $branding.css( 'margin-bottom', '0' );
  97.         }
  98.     }
  99.  
  100.     // Set icon for quotes.
  101.     function setQuotesIcon() {
  102.         $( twentyseventeenScreenReaderText.quote ).prependTo( $formatQuote );
  103.     }
  104.  
  105.     // Add 'below-entry-meta' class to elements.
  106.     function belowEntryMetaClass( param ) {
  107.         var sidebarPos, sidebarPosBottom;
  108.  
  109.         if ( ! $body.hasClass( 'has-sidebar' ) || (
  110.             $body.hasClass( 'search' ) ||
  111.             $body.hasClass( 'single-attachment' ) ||
  112.             $body.hasClass( 'error404' ) ||
  113.             $body.hasClass( 'twentyseventeen-front-page' )
  114.         ) ) {
  115.             return;
  116.         }
  117.  
  118.         sidebarPos       = $sidebar.offset();
  119.         sidebarPosBottom = sidebarPos.top + ( $sidebar.height() + 28 );
  120.  
  121.         $entryContent.find( param ).each( function() {
  122.             var $element = $( this ),
  123.                 elementPos = $element.offset(),
  124.                 elementPosTop = elementPos.top;
  125.  
  126.             // Add 'below-entry-meta' to elements below the entry meta.
  127.             if ( elementPosTop > sidebarPosBottom ) {
  128.                 $element.addClass( 'below-entry-meta' );
  129.             } else {
  130.                 $element.removeClass( 'below-entry-meta' );
  131.             }
  132.         });
  133.     }
  134.  
  135.     /*
  136.      * Test if inline SVGs are supported.
  137.      * @link https://github.com/Modernizr/Modernizr/
  138.      */
  139.     function supportsInlineSVG() {
  140.         var div = document.createElement( 'div' );
  141.         div.innerHTML = '<svg/>';
  142.         return 'http://www.w3.org/2000/svg' === ( 'undefined' !== typeof SVGRect && div.firstChild && div.firstChild.namespaceURI );
  143.     }
  144.  
  145.     /**
  146.      * Test if an iOS device.
  147.     */
  148.     function checkiOS() {
  149.         return /iPad|iPhone|iPod/.test(navigator.userAgent) && ! window.MSStream;
  150.     }
  151.  
  152.     /*
  153.      * Test if background-attachment: fixed is supported.
  154.      * @link http://stackoverflow.com/questions/14115080/detect-support-for-background-attachment-fixed
  155.      */
  156.     function supportsFixedBackground() {
  157.         var el = document.createElement('div'),
  158.             isSupported;
  159.  
  160.         try {
  161.             if ( ! ( 'backgroundAttachment' in el.style ) || checkiOS() ) {
  162.                 return false;
  163.             }
  164.             el.style.backgroundAttachment = 'fixed';
  165.             isSupported = ( 'fixed' === el.style.backgroundAttachment );
  166.             return isSupported;
  167.         }
  168.         catch (e) {
  169.             return false;
  170.         }
  171.     }
  172.  
  173.     // Fire on document ready.
  174.     $( document ).ready( function() {
  175.  
  176.         // If navigation menu is present on page, setNavProps and adjustScrollClass.
  177.         if ( $navigation.length ) {
  178.             setNavProps();
  179.             adjustScrollClass();
  180.         }
  181.  
  182.         // If 'Scroll Down' arrow in present on page, calculate scroll offset and bind an event handler to the click event.
  183.         if ( $menuScrollDown.length ) {
  184.  
  185.             if ( $( 'body' ).hasClass( 'admin-bar' ) ) {
  186.                 menuTop -= 32;
  187.             }
  188.             if ( $( 'body' ).hasClass( 'blog' ) ) {
  189.                 menuTop -= 30; // The div for latest posts has no space above content, add some to account for this.
  190.             }
  191.             if ( ! $navigation.length ) {
  192.                 navigationOuterHeight = 0;
  193.             }
  194.  
  195.             $menuScrollDown.click( function( e ) {
  196.                 e.preventDefault();
  197.                 $( window ).scrollTo( '#primary', {
  198.                     duration: 600,
  199.                     offset: { top: menuTop - navigationOuterHeight }
  200.                 });
  201.             });
  202.         }
  203.  
  204.         adjustHeaderHeight();
  205.         setQuotesIcon();
  206.         if ( true === supportsInlineSVG() ) {
  207.             document.documentElement.className = document.documentElement.className.replace( /(\s*)no-svg(\s*)/, '$1svg$2' );
  208.         }
  209.  
  210.         if ( true === supportsFixedBackground() ) {
  211.             document.documentElement.className += ' background-fixed';
  212.         }
  213.     });
  214.  
  215.     // If navigation menu is present on page, adjust it on scroll and screen resize.
  216.     if ( $navigation.length ) {
  217.  
  218.         // On scroll, we want to stick/unstick the navigation.
  219.         $( window ).on( 'scroll', function() {
  220.             adjustScrollClass();
  221.             adjustHeaderHeight();
  222.         });
  223.  
  224.         // Also want to make sure the navigation is where it should be on resize.
  225.         $( window ).resize( function() {
  226.             setNavProps();
  227.             setTimeout( adjustScrollClass, 500 );
  228.         });
  229.     }
  230.  
  231.     $( window ).resize( function() {
  232.         clearTimeout( resizeTimer );
  233.         resizeTimer = setTimeout( function() {
  234.             belowEntryMetaClass( 'blockquote.alignleft, blockquote.alignright' );
  235.         }, 300 );
  236.         setTimeout( adjustHeaderHeight, 1000 );
  237.     });
  238.  
  239.     // Add header video class after the video is loaded.
  240.     $( document ).on( 'wp-custom-header-video-loaded', function() {
  241.         $body.addClass( 'has-header-video' );
  242.     });
  243.  
  244. })( jQuery );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement