front-page.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ( function( $ ) {
  2. /*
  3. * Since we've absolutely positioned the header (so it sits on top of the background
  4. * set for our hero panel), we're going to need to adjust the margin of our hero's
  5. * entry-content so it fits correctly in the available space.
  6. */
  7. function adjustHero() {
  8. var piqueHeaderHeight = $( '#masthead' ).height();
  9. var piqueHeroContent = $( '#pique-hero' ).find( '.pique-panel-content' );
  10. $( piqueHeroContent ).css( 'padding-top', piqueHeaderHeight );
  11. }
  12. /*
  13. * We're going to use the Waypoints and ScrollTo libraries to build a dynamic menu.
  14. * This will animate a scroll effect to and from panels in the page as well as
  15. * highlighting the current panel in the nav menu.
  16. */
  17. function dynamicNav() {
  18. var $sections = $( '.pique-panel' );
  19. var $navLinks = $( '#site-navigation li a' );
  20. var $primaryMenu = $( '#primary-menu' );
  21. var $adminBar = $( '#wpadminbar' );
  22. // Use the Waypoints plugin to indicate our current nav item
  23. $sections.waypoint( {
  24. handler: function( direction ) {
  25. var activePanel = this;
  26. var panelID = activePanel.element.id;
  27. // If we're scrolling up, set the previous panel as our current panel.
  28. // This just means that, when we hit to top of a panel (bottom of a new panel)
  29. // we'll highlight the correct panel. Wheee!
  30. if ( 'up' === direction ) {
  31. // Subtract twice because waypoints is one-indexed; DOM is zero-indexed.
  32. var elementIndex = activePanel.key.substr( 9, 1 ) - 2;
  33. panelID = $sections.eq( elementIndex ).attr('id');
  34. }
  35. // Don't show any highlight for our parent nav
  36. if ( 'pique-hero' === panelID ) {
  37. $navLinks.parents( 'li' ).removeClass( 'current_page_item' );
  38. }
  39. // Find the active panel's corresponding link by matching the panel ID in the URL
  40. var activeLink = $( 'nav a[href="#' + panelID + '"]' ).parent( 'li' );
  41. // Remove any existing classes
  42. $navLinks.parents( 'li' ).removeClass( 'current_page_item' );
  43. // Highlight the currently active panel by adding a CSS class
  44. activeLink.addClass( 'current_page_item' );
  45. },
  46. offset: '110px'
  47. });
  48. // Use scrollTo library to smoothly scroll between panels.
  49. // Note that the admin bar lenght is only taken into consideration on screen size > 480px, when it becomes fixed.
  50. // Also, we take into account he height of the primary menu after 768px.
  51. $navLinks.click( function() {
  52. var yCoord = ( 1 === $adminBar.length && $( window ).outerWidth() > 480 ) ? $adminBar.outerHeight() : 0;
  53. var offset = $( window ).outerWidth() > 768 ? $primaryMenu.outerHeight() : 0;
  54. $.scrollTo( $( this ).attr( 'href' ), {
  55. duration: 400,
  56. offset: { 'top': -1 * ( yCoord + offset ) }
  57. } );
  58. });
  59. }
  60. // Run our functions on document load
  61. $( window ).on( 'load', function() {
  62. adjustHero();
  63. dynamicNav();
  64. });
  65. // Annnnnd re-run the hero adjustment every time the window resizes
  66. var isResizing = false;
  67. $( window ).on('resize', function() {
  68. if (isResizing) {
  69. return;
  70. }
  71. isResizing = true;
  72. setTimeout(function() {
  73. adjustHero();
  74. isResizing = false;
  75. }, 150);
  76. });
  77. } )( jQuery );