functions.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * Theme functions file
  3. *
  4. * Contains handlers for navigation, accessibility, header sizing
  5. * footer widgets and Featured Content slider
  6. *
  7. */
  8. ( function( $ ) {
  9. var body = $( 'body' ),
  10. _window = $( window ),
  11. nav, button, menu;
  12. nav = $( '#primary-navigation' );
  13. button = nav.find( '.menu-toggle' );
  14. menu = nav.find( '.nav-menu' );
  15. // Enable menu toggle for small screens.
  16. ( function() {
  17. if ( ! nav || ! button ) {
  18. return;
  19. }
  20. // Hide button if menu is missing or empty.
  21. if ( ! menu || ! menu.children().length ) {
  22. button.hide();
  23. return;
  24. }
  25. button.on( 'click.twentyfourteen', function() {
  26. nav.toggleClass( 'toggled-on' );
  27. if ( nav.hasClass( 'toggled-on' ) ) {
  28. $( this ).attr( 'aria-expanded', 'true' );
  29. menu.attr( 'aria-expanded', 'true' );
  30. } else {
  31. $( this ).attr( 'aria-expanded', 'false' );
  32. menu.attr( 'aria-expanded', 'false' );
  33. }
  34. } );
  35. } )();
  36. /*
  37. * Makes "skip to content" link work correctly in IE9 and Chrome for better
  38. * accessibility.
  39. *
  40. * @link http://www.nczonline.net/blog/2013/01/15/fixing-skip-to-content-links/
  41. */
  42. _window.on( 'hashchange.twentyfourteen', function() {
  43. var hash = location.hash.substring( 1 ), element;
  44. if ( ! hash ) {
  45. return;
  46. }
  47. element = document.getElementById( hash );
  48. if ( element ) {
  49. if ( ! /^(?:a|select|input|button|textarea)$/i.test( element.tagName ) ) {
  50. element.tabIndex = -1;
  51. }
  52. element.focus();
  53. // Repositions the window on jump-to-anchor to account for header height.
  54. window.scrollBy( 0, -80 );
  55. }
  56. } );
  57. $( function() {
  58. // Search toggle.
  59. $( '.search-toggle' ).on( 'click.twentyfourteen', function( event ) {
  60. var that = $( this ),
  61. wrapper = $( '#search-container' ),
  62. container = that.find( 'a' );
  63. that.toggleClass( 'active' );
  64. wrapper.toggleClass( 'hide' );
  65. if ( that.hasClass( 'active' ) ) {
  66. container.attr( 'aria-expanded', 'true' );
  67. } else {
  68. container.attr( 'aria-expanded', 'false' );
  69. }
  70. if ( that.is( '.active' ) || $( '.search-toggle .screen-reader-text' )[0] === event.target ) {
  71. wrapper.find( '.search-field' ).focus();
  72. }
  73. } );
  74. /*
  75. * Fixed header for large screen.
  76. * If the header becomes more than 48px tall, unfix the header.
  77. *
  78. * The callback on the scroll event is only added if there is a header
  79. * image and we are not on mobile.
  80. */
  81. if ( _window.width() > 781 ) {
  82. var mastheadHeight = $( '#masthead' ).height(),
  83. toolbarOffset, mastheadOffset;
  84. if ( mastheadHeight > 48 ) {
  85. body.removeClass( 'masthead-fixed' );
  86. }
  87. if ( body.is( '.header-image' ) ) {
  88. toolbarOffset = body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0;
  89. mastheadOffset = $( '#masthead' ).offset().top - toolbarOffset;
  90. _window.on( 'scroll.twentyfourteen', function() {
  91. if ( _window.scrollTop() > mastheadOffset && mastheadHeight < 49 ) {
  92. body.addClass( 'masthead-fixed' );
  93. } else {
  94. body.removeClass( 'masthead-fixed' );
  95. }
  96. } );
  97. }
  98. }
  99. // Focus styles for menus.
  100. $( '.primary-navigation, .secondary-navigation' ).find( 'a' ).on( 'focus.twentyfourteen blur.twentyfourteen', function() {
  101. $( this ).parents().toggleClass( 'focus' );
  102. } );
  103. } );
  104. /**
  105. * @summary Add or remove ARIA attributes.
  106. * Uses jQuery's width() function to determine the size of the window and add
  107. * the default ARIA attributes for the menu toggle if it's visible.
  108. * @since Twenty Fourteen 1.4
  109. */
  110. function onResizeARIA() {
  111. if ( 781 > _window.width() ) {
  112. button.attr( 'aria-expanded', 'false' );
  113. menu.attr( 'aria-expanded', 'false' );
  114. button.attr( 'aria-controls', 'primary-menu' );
  115. } else {
  116. button.removeAttr( 'aria-expanded' );
  117. menu.removeAttr( 'aria-expanded' );
  118. button.removeAttr( 'aria-controls' );
  119. }
  120. }
  121. _window
  122. .on( 'load.twentyfourteen', onResizeARIA )
  123. .on( 'resize.twentyfourteen', function() {
  124. onResizeARIA();
  125. } );
  126. _window.load( function() {
  127. // Arrange footer widgets vertically.
  128. if ( $.isFunction( $.fn.masonry ) ) {
  129. $( '#footer-sidebar' ).masonry( {
  130. itemSelector: '.widget',
  131. columnWidth: function( containerWidth ) {
  132. return containerWidth / 4;
  133. },
  134. gutterWidth: 0,
  135. isResizable: true,
  136. isRTL: $( 'body' ).is( '.rtl' )
  137. } );
  138. }
  139. // Initialize Featured Content slider.
  140. if ( body.is( '.slider' ) ) {
  141. $( '.featured-content' ).featuredslider( {
  142. selector: '.featured-content-inner > article',
  143. controlsContainer: '.featured-content'
  144. } );
  145. }
  146. } );
  147. } )( jQuery );