posts.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. ( function( $ ) {
  2. var debounce = function( func, wait ) {
  3. var timeout, args, context, timestamp;
  4. return function() {
  5. context = this;
  6. args = [].slice.call( arguments, 0 );
  7. timestamp = new Date();
  8. var later = function() {
  9. var last = ( new Date() ) - timestamp;
  10. if ( last < wait ) {
  11. timeout = setTimeout( later, wait - last );
  12. } else {
  13. timeout = null;
  14. func.apply( context, args );
  15. }
  16. };
  17. if ( ! timeout ) {
  18. timeout = setTimeout( later, wait );
  19. }
  20. };
  21. };
  22. function postsStyles() {
  23. var marginSize = 30;
  24. if ( $( window ).width() > 599 ) {
  25. marginSize = 60;
  26. }
  27. $( '.site-main .hentry' ).each( function() {
  28. if ( $( this ).hasClass( 'has-post-thumbnail' ) && ( $( this ).hasClass( 'format-image' ) || $( this ).hasClass( 'format-gallery' ) ) && ! $( this ).parent().hasClass( 'featured-content' ) ) {
  29. var postThumbnail = $( this ).find( '.post-thumbnail' ),
  30. thumbnail = $( this ).find( 'img' );
  31. postThumbnail.css( {
  32. 'background-image': 'url(' + thumbnail.attr( 'src' ) + ')',
  33. 'height': $( this ).outerHeight() - marginSize
  34. } );
  35. }
  36. } );
  37. }
  38. $( window ).load( postsStyles ).resize( debounce( postsStyles, 500 ) );
  39. $( document ).on( 'post-load', postsStyles );
  40. } )( jQuery );