functions.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * Handles sticky navigation for larger screens
  3. */
  4. ( function( $ ) {
  5. var body, resizeTimer, currentPosition, header, headerImg;
  6. body = $( document.body );
  7. if ( window.navigator.userAgent.indexOf("Edge") > -1 || window.navigator.userAgent.indexOf("Trident/") > -1 ) {
  8. body.addClass( 'is-edge' );
  9. }
  10. // Add 'below-entry-meta' class to elements.
  11. function belowEntryMetaClass( param ) {
  12. if ( body.hasClass( 'search' ) ||
  13. body.hasClass( 'single-attachment' ) ||
  14. body.hasClass( 'error404' ) ) {
  15. return;
  16. }
  17. $( '.entry-content' ).find( param ).each( function() {
  18. var element = $( this ),
  19. elementPos = element.offset(),
  20. elementPosTop = elementPos.top,
  21. entryMeta = element.closest( 'article' ).find( '.entry-meta' ),
  22. entryMetaPos, entryMetaPosBottom;
  23. if ( 0 != entryMeta.length ) { //If entry meta exists
  24. entryMetaPos = entryMeta.offset(),
  25. entryMetaPosBottom = entryMetaPos.top + ( entryMeta.height() + 28 );
  26. }
  27. else {
  28. entryMetaPos = 0,
  29. entryMetaPosBottom = 0;
  30. }
  31. // Add 'below-entry-meta' to elements below the entry meta.
  32. if ( elementPosTop > entryMetaPosBottom ) {
  33. element.addClass( 'below-entry-meta' );
  34. } else {
  35. element.removeClass( 'below-entry-meta' );
  36. }
  37. } );
  38. }
  39. // Add 'below-sidebar' class to elements.
  40. function belowSidebarClass( param ) {
  41. if ( body.hasClass( 'search' ) ||
  42. body.hasClass( 'single-attachment' ) ||
  43. body.hasClass( 'error404' ) ) {
  44. return;
  45. }
  46. $( '.entry-content' ).find( param ).each( function() {
  47. var element = $( this ),
  48. elementPos = element.offset(),
  49. elementPosTop = elementPos.top,
  50. sidebar = $( '#secondary.widget-area' ),
  51. sidebarPos, sidebarPosBottom;
  52. if ( 0 != sidebar.length ) { //If a sidebar exists
  53. sidebarPos = sidebar.offset();
  54. sidebarPosBottom = sidebarPos.top + ( sidebar.height() + 28 );
  55. }
  56. else {
  57. sidebarPos = 0;
  58. sidebarPosBottom = 0;
  59. }
  60. // Add 'below-sidebar' to elements below the sidebar.
  61. if ( elementPosTop > sidebarPosBottom ) {
  62. element.addClass( 'below-sidebar' );
  63. } else {
  64. element.removeClass( 'below-sidebar' );
  65. }
  66. } );
  67. }
  68. //Adjust width of padding on Jetpack Contact Form labels
  69. function formLabelLength() {
  70. var form = $( '.entry-content .contact-form' );
  71. if ( 0 == form.length ) {
  72. return;
  73. }
  74. form.each( function() {
  75. var labels,
  76. inputs,
  77. widths,
  78. padding;
  79. // Find all form labels and inputs
  80. labels = $( this ).find( 'label.text, label.url, label.name, label.email' );
  81. inputs = $( this ).find( 'input[type="text"], input[type="email"], input[type="url"], input[type="name"]' );
  82. if ( 0 !== labels.length ) {
  83. widths = new Array();
  84. // Get the width of each label
  85. labels.each( function() {
  86. if ( $( this ).parent().width() - $( this ).width() > 100 ) {
  87. widths.push( $( this ).outerWidth() );
  88. $( this ).addClass( 'absolute' );
  89. } else {
  90. $( this ).next( 'input[type="text"], input[type="email"], input[type="url"], input[type="name"]' ).addClass( 'no-padding' );
  91. }
  92. } );
  93. // Get longest value and add extra for padding
  94. padding = Math.max.apply( Math, widths ) + 28;
  95. // Set right/left-hand padding on the fields
  96. if ( $( 'html' ).attr( 'dir' ) == 'rtl') {
  97. inputs.css( { 'padding-right' : padding } );
  98. } else {
  99. inputs.css( { 'padding-left' : padding } );
  100. }
  101. }
  102. } );
  103. }
  104. //Apply classes to aligned left, right, wide, and full elements (blockquotes, captions, images)
  105. function moveAlignedElements() {
  106. var ltr = true;
  107. if ( $( 'html' ).attr( 'dir' ) == 'rtl') {
  108. ltr = false;
  109. }
  110. $( window )
  111. .on( 'resize post-load', function() {
  112. clearTimeout( resizeTimer );
  113. resizeTimer = setTimeout( function() {
  114. belowEntryMetaClass( 'figure.wp-block-image.alignwide' );
  115. belowSidebarClass( 'figure.wp-block-image.alignfull, figure.wp-block-image.alignwide' );
  116. belowEntryMetaClass( '.wp-block-cover.alignwide, .wp-block-gallery.alignwide' );
  117. belowSidebarClass( '.wp-block-cover.alignfull, .wp-block-cover.alignwide, .wp-block-gallery.alignfull, .wp-block-gallery.alignwide' );
  118. belowEntryMetaClass( '.wp-block-embed.is-type-video.alignwide' );
  119. belowSidebarClass( '.wp-block-embed.is-type-video.alignfull, .wp-block-embed.is-type-video.alignwide' );
  120. if ( true == ltr ) {
  121. belowEntryMetaClass( 'img.alignleft:not( figure > img.alignleft ), figure.alignleft' );
  122. belowEntryMetaClass( 'blockquote.alignleft' );
  123. belowSidebarClass( 'img.alignright:not( figure > img.alignright ), figure.alignright' );
  124. belowSidebarClass( 'blockquote.alignright' );
  125. }
  126. else {
  127. belowEntryMetaClass( 'img.alignright:not( figure > img.alignright ), figure.alignright' );
  128. belowEntryMetaClass( 'blockquote.alignright' );
  129. belowSidebarClass( 'img.alignleft:not( figure > img.alignleft ), figure.alignleft' );
  130. belowSidebarClass( 'blockquote.alignleft' );
  131. }
  132. if ( $( window ).width() >= 736 ) {
  133. headerImg.css( 'min-height', header.outerHeight() + 'px' );
  134. }
  135. }, 300 );
  136. } );
  137. belowEntryMetaClass( 'figure.wp-block-image.alignwide' );
  138. belowSidebarClass( 'figure.wp-block-image.alignfull, figure.wp-block-image.alignwide' );
  139. belowEntryMetaClass( '.wp-block-cover.alignwide, .wp-block-gallery.alignwide' );
  140. belowSidebarClass( '.wp-block-cover.alignfull, .wp-block-cover.alignwide, .wp-block-gallery.alignfull, .wp-block-gallery.alignwide' );
  141. belowEntryMetaClass( '.wp-block-embed.is-type-video.alignwide' );
  142. belowSidebarClass( '.wp-block-embed.is-type-video.alignfull, .wp-block-embed.is-type-video.alignwide' );
  143. if ( true == ltr ) {
  144. belowEntryMetaClass( 'img.alignleft:not( figure > img.alignleft ), figure.alignleft' );
  145. belowEntryMetaClass( 'blockquote.alignleft' );
  146. belowSidebarClass( 'img.alignright:not( figure > img.alignright ), figure.alignright' );
  147. belowSidebarClass( 'blockquote.alignright' );
  148. }
  149. else {
  150. belowEntryMetaClass( 'img.alignright:not( figure > img.alignright ), figure.alignright' );
  151. belowEntryMetaClass( 'blockquote.alignright' );
  152. belowSidebarClass( 'img.alignleft:not( figure > img.alignleft ), figure.alignleft' );
  153. belowSidebarClass( 'blockquote.alignleft' );
  154. }
  155. }
  156. //Resize the header to fit its contents
  157. function sizeHeader() {
  158. if ( $( window ).width() < 736 ) {
  159. return;
  160. }
  161. currentPosition = window.pageYOffset;
  162. header = $( '.header-wrapper' );
  163. headerImg = $( '.site-header .custom-header' );
  164. if ( currentPosition > ( headerImg.height() + 32 ) ) {
  165. header.addClass( 'without-transparency' );
  166. }
  167. headerImg.css( 'min-height', header.outerHeight() + 'px' );
  168. }
  169. //Give header navigation a background color when scrolling past the header
  170. function headerTransparency() {
  171. currentPosition = window.pageYOffset;
  172. header = $( '.header-wrapper' );
  173. headerImg = $( '.site-header .custom-header' );
  174. if ( currentPosition > ( headerImg.height() - header.height() ) ) {
  175. header.addClass( 'without-transparency' );
  176. } else {
  177. header.removeClass( 'without-transparency' );
  178. }
  179. }
  180. //Masonry for Guestbook template
  181. function guestbookMasonry() {
  182. body = $( document.body );
  183. if ( ! body.hasClass( 'page-template-guestbook' ) ) {
  184. return;
  185. }
  186. var ltr = true;
  187. if ( $( 'html' ).attr( 'dir' ) == 'rtl') {
  188. ltr = false;
  189. }
  190. var $guestbookContainer = $( '.page-template-guestbook .comments-area .comment-list');
  191. $guestbookContainer.imagesLoaded( function() {
  192. $guestbookContainer.masonry( {
  193. itemSelector: '.comment',
  194. isOriginLeft: ltr,
  195. gutter: 30,
  196. } );
  197. } );
  198. }
  199. /* Functions to fire on document ready */
  200. $( document ).ready( function() {
  201. moveAlignedElements();
  202. } );
  203. /* Functions to fire on window load */
  204. $( window ).load( function() {
  205. guestbookMasonry();
  206. formLabelLength();
  207. sizeHeader();
  208. } );
  209. /* Functions to fire on infinite scroll's post-load */
  210. $( window ).on( 'post-load', function() {
  211. formLabelLength();
  212. } );
  213. /* Functions to fire on resize */
  214. $( window ).on( 'resize', function() {
  215. sizeHeader();
  216. } );
  217. /* Functions to fire on document scroll */
  218. $( document ).on( 'scroll', function() {
  219. headerTransparency();
  220. });
  221. } )( jQuery );