functions.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // Enable menu toggle for small screens.
  12. ( function() {
  13. var nav = $( '#primary-navigation' ), button, menu;
  14. if ( ! nav ) {
  15. return;
  16. }
  17. button = nav.find( '.menu-toggle' );
  18. if ( ! button ) {
  19. return;
  20. }
  21. // Hide button if menu is missing or empty.
  22. menu = nav.find( '.nav-menu' );
  23. if ( ! menu || ! menu.children().length ) {
  24. button.hide();
  25. return;
  26. }
  27. $( '.menu-toggle' ).on( 'click.twentyfourteen', function() {
  28. nav.toggleClass( 'toggled-on' );
  29. } );
  30. } )();
  31. /*
  32. * Makes "skip to content" link work correctly in IE9 and Chrome for better
  33. * accessibility.
  34. *
  35. * @link http://www.nczonline.net/blog/2013/01/15/fixing-skip-to-content-links/
  36. */
  37. _window.on( 'hashchange.twentyfourteen', function() {
  38. var hash = location.hash.substring( 1 ), element;
  39. if ( ! hash ) {
  40. return;
  41. }
  42. element = document.getElementById( hash );
  43. if ( element ) {
  44. if ( ! /^(?:a|select|input|button|textarea)$/i.test( element.tagName ) ) {
  45. element.tabIndex = -1;
  46. }
  47. element.focus();
  48. // Repositions the window on jump-to-anchor to account for header height.
  49. window.scrollBy( 0, -80 );
  50. }
  51. } );
  52. $( function() {
  53. // Search toggle.
  54. $( '.search-toggle' ).on( 'click.twentyfourteen', function( event ) {
  55. var that = $( this ),
  56. wrapper = $( '.search-box-wrapper' );
  57. that.toggleClass( 'active' );
  58. wrapper.toggleClass( 'hide' );
  59. if ( that.is( '.active' ) || $( '.search-toggle .screen-reader-text' )[0] === event.target ) {
  60. wrapper.find( '.search-field' ).focus();
  61. }
  62. } );
  63. /*
  64. * Fixed header for large screen.
  65. * If the header becomes more than 48px tall, unfix the header.
  66. *
  67. * The callback on the scroll event is only added if there is a header
  68. * image and we are not on mobile.
  69. */
  70. if ( _window.width() > 781 ) {
  71. var mastheadHeight = $( '#masthead' ).height(),
  72. toolbarOffset, mastheadOffset;
  73. if ( mastheadHeight > 48 ) {
  74. body.removeClass( 'masthead-fixed' );
  75. }
  76. if ( body.is( '.header-image' ) ) {
  77. toolbarOffset = body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0;
  78. mastheadOffset = $( '#masthead' ).offset().top - toolbarOffset;
  79. _window.on( 'scroll.twentyfourteen', function() {
  80. if ( _window.scrollTop() > mastheadOffset && mastheadHeight < 49 ) {
  81. body.addClass( 'masthead-fixed' );
  82. } else {
  83. body.removeClass( 'masthead-fixed' );
  84. }
  85. } );
  86. }
  87. }
  88. // Focus styles for menus.
  89. $( '.primary-navigation, .secondary-navigation' ).find( 'a' ).on( 'focus.twentyfourteen blur.twentyfourteen', function() {
  90. $( this ).parents().toggleClass( 'focus' );
  91. } );
  92. } );
  93. _window.load( function() {
  94. // Arrange footer widgets vertically.
  95. if ( $.isFunction( $.fn.masonry ) ) {
  96. $( '#footer-sidebar' ).masonry( {
  97. itemSelector: '.widget',
  98. columnWidth: function( containerWidth ) {
  99. return containerWidth / 4;
  100. },
  101. gutterWidth: 0,
  102. isResizable: true,
  103. isRTL: $( 'body' ).is( '.rtl' )
  104. } );
  105. }
  106. // Initialize Featured Content slider.
  107. if ( body.is( '.slider' ) ) {
  108. $( '.featured-content' ).featuredslider( {
  109. selector: '.featured-content-inner > article',
  110. controlsContainer: '.featured-content'
  111. } );
  112. }
  113. } );
  114. } )( jQuery );