photon.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* jshint onevar: false */
  2. (function($){
  3. /**
  4. * For images lacking explicit dimensions and needing them, try to add them.
  5. */
  6. var restore_dims = function() {
  7. $( 'img[data-recalc-dims]' ).each( function recalc() {
  8. var $this = $( this );
  9. if ( this.complete ) {
  10. // Support for lazy loading: if there is a lazy-src
  11. // attribute and it's value is not the same as the current src we
  12. // should wait until the image load event
  13. if ( $this.data( 'lazy-src' ) && $this.attr( 'src' ) !== $this.data( 'lazy-src' ) ) {
  14. $this.load( recalc );
  15. return;
  16. }
  17. var width = this.width,
  18. height = this.height;
  19. if ( width && width > 0 && height && height > 0 ) {
  20. $this.attr( {
  21. width: width,
  22. height: height
  23. } );
  24. reset_for_retina( this );
  25. }
  26. }
  27. else {
  28. $this.load( recalc );
  29. }
  30. } );
  31. },
  32. /**
  33. * Modify given image's markup so that devicepx-jetpack.js will act on the image and it won't be reprocessed by this script.
  34. */
  35. reset_for_retina = function( img ) {
  36. $( img ).removeAttr( 'data-recalc-dims' ).removeAttr( 'scale' );
  37. };
  38. /**
  39. * Check both when page loads, and when IS is triggered.
  40. */
  41. $( document ).ready( restore_dims );
  42. if ( 'on' in $.fn ) {
  43. $( document.body ).on( 'post-load', restore_dims );
  44. } else {
  45. $( document ).delegate( 'body', 'post-load', restore_dims );
  46. }
  47. })(jQuery);