global.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * Theme frontend scripts
  3. *
  4. * @package dyad
  5. *
  6. */
  7. ( function( $ ) {
  8. var $window = $( window );
  9. $( '.no-js' ).removeClass( 'no-js' );
  10. /**
  11. * Banner slider
  12. */
  13. if ( $().slick ) {
  14. $( '#site-banner.enable-slider .site-banner-inner' ).slick( {
  15. 'adaptiveHeight' : false,
  16. 'autoplay' : true,
  17. 'autoplaySpeed' : ( ! jQuery( '#site-banner' ).data( 'speed' ) ) ? ( 5400 ) : ( jQuery( '#site-banner' ).data( 'speed' ) ),
  18. 'cssEase' : 'ease-in-out',
  19. 'dots' : false,
  20. 'draggable' : false,
  21. 'easing' : 'easeInOutBack',
  22. 'fade' : true,
  23. 'pauseOnHover' : true,
  24. 'slide' : 'article',
  25. 'speed' : 600,
  26. 'swipeToSlide' : true,
  27. 'prevArrow' : '<div class="slider-nav slider-nav-prev"><button type="button" class="slick-prev"><span class="genericon genericon-expand"></span></button></div>',
  28. 'nextArrow' : '<div class="slider-nav slider-nav-next"><button type="button" class="slick-next"><span class="genericon genericon-expand"></span></button></div>'
  29. } );
  30. }
  31. /**
  32. * Page scrolled?
  33. */
  34. if ( 0 == $window.scrollTop() ) {
  35. $( 'body' ).addClass( 'not-scrolled' )
  36. }
  37. $window.on( 'scroll', function( e ) {
  38. if ( 0 == $window.scrollTop() ) {
  39. $( 'body' ).addClass( 'not-scrolled' ).removeClass( 'is-scrolled' );
  40. } else {
  41. $( 'body' ).addClass( 'is-scrolled' ).removeClass( 'not-scrolled' );
  42. }
  43. } );
  44. /*
  45. * Add 'focus' style to contact form
  46. */
  47. $('.comment-form-author input, .comment-form-email input, .comment-form-url input, .comment-form-comment textarea').focus( function() {
  48. $( this ).parent().addClass( 'focus' );
  49. } ).blur( function() {
  50. if( "" == $( this). val() ) {
  51. $( this ).parent().removeClass( 'focus' );
  52. }
  53. } );
  54. /*
  55. * Make sure 'Add Yours' comment link doesn't overshoot the form when header is fixed
  56. */
  57. $( '.add-comment-link' ).click( function( e ) {
  58. if( $( window ).width() > 1400 ) {
  59. e.preventDefault();
  60. var offset = $( '#respond' ).offset();
  61. var scrollto = offset.top - ( $('#masthead').innerHeight() + 50 );
  62. $('html, body').animate({scrollTop:scrollto}, 0);
  63. }
  64. } );
  65. /**
  66. * Make sure content isn't too high in grid view
  67. */
  68. function adjustPosts() {
  69. $('.posts .entry-inner').each( function() {
  70. var $contain = $(this),
  71. $innerContainHeight = $('.entry-inner-content', this ).height(),
  72. $linkMoreHeight = $( '.link-more', this ).height();
  73. $header = $('.entry-header', this),
  74. $headerHeight = $header.innerHeight(),
  75. $content = $('.entry-content', this),
  76. $contentHeight = $content.innerHeight(),
  77. $wholeContentHeight = $headerHeight + $contentHeight;
  78. if ( ( $innerContainHeight - $linkMoreHeight ) < $wholeContentHeight ) {
  79. $contain.parent().addClass('too-short');
  80. } else {
  81. $contain.parent().removeClass('too-short');
  82. }
  83. } );
  84. }
  85. /**
  86. * Adjust header height
  87. */
  88. function adjustHeaderHeight() {
  89. var $header = $('#masthead'),
  90. $headerHeight = $header.outerHeight() + 50;
  91. if( $('body').hasClass('blog') || $('body').hasClass('home') ) {
  92. if( $('.site-banner-header').length !== 0 && $('.site-banner-header').offset().top <= $headerHeight ) {
  93. $('.site-banner').addClass('too-tall');
  94. }
  95. } else {
  96. $('body:not(.blog, .home, .single-format-image.has-post-thumbnail) .site-content').css('padding-top', $headerHeight );
  97. }
  98. }
  99. /**
  100. * Masonry for footer widgets
  101. */
  102. function widgetMasonry() {
  103. // Make sure we're on a larger screen
  104. if ( 'none' !== $( '#colophon .widget-area .widget:first-of-type' ).css( 'float' ) ) {
  105. // Determine text direction
  106. var ltr = true;
  107. if ( $('html' ).attr( 'dir' ) == 'rtl') {
  108. ltr = false;
  109. }
  110. $grid = $( '.grid-container' ).masonry( {
  111. itemSelector: '.widget',
  112. columnWidth: '.widget-area aside',
  113. isOriginLeft: ltr,
  114. } );
  115. // Re-fire masonry after set of time, for any late-loading widgets
  116. setTimeout( function() { $grid.masonry( 'layout' ); }, 2000 );
  117. }
  118. }
  119. /**
  120. * Firing events
  121. */
  122. // Fire on load
  123. $( window ).on( 'load', function() {
  124. widgetMasonry();
  125. } );
  126. // Fire on document ready
  127. $( document ).ready(function(){
  128. adjustPosts();
  129. adjustHeaderHeight();
  130. // Make sure we're on a larger screen
  131. if ( 'none' !== $( '#colophon .widget-area .widget:first-of-type' ).css( 'float' ) ) {
  132. // Set some min-heights to help improve Masonry's treatment of these widgets
  133. // Get Twitter widgets and set a min-height on parent elements
  134. $( 'a.twitter-timeline' ).each( function() {
  135. var thisHeight = $( this ).attr( 'data-height' );
  136. // Set the widget to have this height
  137. $( this ).parent().css( 'min-height', thisHeight + 'px' );
  138. } );
  139. // Get Facebook widgets and set a min-height on parent elements
  140. $( '.fb-page' ).each( function() {
  141. // Get some settings from the initial markup:
  142. var $set_height = $( this ).data( 'height' ),
  143. $hide_cover = $( this ).data( 'hide-cover' ),
  144. $show_facepile = $( this ).data( 'show-facepile' ),
  145. $show_posts = $( this ).data( 'show-posts' ), // AKA stream
  146. $min_height = $set_height; // set the default 'min-height'
  147. // These values are defaults from the FB widget.
  148. var $no_posts_no_faces = 130,
  149. $no_posts = 220;
  150. if ( $show_posts ) {
  151. // Showing posts; may also be showing faces and/or cover - the latter doesn't affect the height at all.
  152. $min_height = $set_height;
  153. } else if ( $show_facepile ) {
  154. // Showing facepile with or without cover image - both would be same height.
  155. // If the user selected height is lower than the no_posts height, we'll use that instead
  156. $min_height = ( $set_height < $no_posts ) ? $set_height : $no_posts;
  157. } else {
  158. // Either just showing cover, or nothing is selected (both are same height).
  159. // If the user selected height is lower than the no_posts_no_faces height, we'll use that instead
  160. $min_height = ( $set_height < $no_posts_no_faces ) ? $set_height : $no_posts_no_faces;
  161. }
  162. // apply min-height to .fb-page container
  163. $( this ).css( 'min-height', $min_height + 'px' );
  164. } );
  165. }
  166. } );
  167. //Fire on window resize
  168. var resizeTimer;
  169. $( window ).on( 'resize', function() {
  170. clearTimeout( resizeTimer );
  171. resizeTimer = setTimeout( function() {
  172. adjustPosts();
  173. adjustHeaderHeight();
  174. // Fire Masonry, in case we're moving from mobile to desktop
  175. widgetMasonry();
  176. }, 250 );
  177. } );
  178. // if Infinite Scroll, fire again when new posts are loaded
  179. $( document.body ).on( 'post-load', function() {
  180. adjustPosts();
  181. } );
  182. } )( jQuery );