posts.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. $( '.hentry' ).each( function() {
  24. var entryMeta = $( this ).first( '.entry-meta' ),
  25. catLinks = entryMeta.find( '.cat-links' ),
  26. commentsLink = entryMeta.find( '.comments-link' );
  27. if ( catLinks.width() + commentsLink.width() >= entryMeta.width() ) {
  28. $( this ).addClass( 'long-meta' );
  29. } else {
  30. $( this ).removeClass( 'long-meta' );
  31. }
  32. if ( ! $( this ).hasClass( 'background-done' ) && $( this ).hasClass( 'has-post-thumbnail' ) && ( $( this ).hasClass( 'format-image' ) || $( this ).hasClass( 'format-gallery' ) ) && ! $( this ).parent().hasClass( 'featured-content' ) ) {
  33. var entryImage = $( this ).find( '.post-thumbnail' ),
  34. thumbnail = $( this ).find( 'img' );
  35. if ( $.trim( $( this ).find( '.entry-summary' ).text() ) === '' ) {
  36. $( this ).find( '.entry-summary' ).remove();
  37. $( this ).addClass( 'no-summary' );
  38. }
  39. entryImage.css( {
  40. 'background-image': 'url(' + thumbnail.attr( 'src' ) + ')',
  41. 'height': $( this ).outerHeight() - $( this ).find( '.entry-meta' ).height() - 15,
  42. 'top': $( this ).find( '.entry-meta' ).outerHeight() + 15
  43. } );
  44. $( this ).addClass( 'background-done' );
  45. }
  46. } );
  47. }
  48. $( window ).load( postsStyles ).resize( debounce( postsStyles, 500 ) );
  49. $( document ).on( 'post-load', postsStyles );
  50. } )( jQuery );