featured-content.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. var featuredContent, header, primary;
  23. featuredContent = $( '#featured-content' );
  24. header = $( '#masthead' );
  25. primary = $( '#primary' );
  26. if ( ! featuredContent.length ) {
  27. return;
  28. }
  29. /* Use Featured Image as a Background Image */
  30. featuredContent.find( '.hentry' ).each( function() {
  31. if ( ! $( this ).hasClass( 'background-done' ) && $( this ).hasClass( 'has-post-thumbnail' ) ) {
  32. var entryImage = $( this ).find( '.post-thumbnail' ),
  33. thumbnail = $( this ).find( 'img' );
  34. entryImage.css( 'background-image', 'url(' + thumbnail.attr( 'src' ) + ')' );
  35. $( this ).addClass( 'background-done' );
  36. }
  37. } );
  38. if ( ! header.length || ! primary.length ) {
  39. return;
  40. }
  41. /* Move Featured Content in the DOM depending on the screen size */
  42. function featuredContentPosition() {
  43. if ( $( window ).width() > 959 ) {
  44. featuredContent.insertBefore( header );
  45. } else {
  46. featuredContent.insertBefore( primary );
  47. }
  48. featuredContent.show();
  49. }
  50. $( window ).load( featuredContentPosition ).resize( debounce( featuredContentPosition, 500 ) );
  51. } )( jQuery );