Skip to content

Commit

Permalink
Fixes incorrect srcset reevaluation on resize.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilto committed Jun 18, 2012
1 parent d8f45e2 commit d275823
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions picturefill.js
Expand Up @@ -60,25 +60,28 @@
}

if( srcset ) {
var screenRes = w.devicePixelRatio || 1, // Is it worth looping through reasonable matchMedia values here?
sources = srcset.split(","); // Split comma-separated `srcset` sources into an array.
var screenRes = w.devicePixelRatio || 1, // Is it worth looping through reasonable matchMedia values here?
sources = srcset.split(","); // Split comma-separated `srcset` sources into an array.

for( var res = sources.length, r = res - 1; r >= 0; r-- ) { // Loop through each source/resolution in `srcset`.
var source = sources[ r ].replace(/^\s*/, '').replace(/\s*$/, '').split(" "), // Remove any leading whitespace, then split on spaces.
resMatch = parseFloat( source[1], 10 ); // Parse out the resolution for each source in `srcset`.
for( var res = sources.length, r = res - 1; r >= 0; r-- ) { // Loop through each source/resolution in `srcset`.
var source = sources[ r ].replace(/^\s*/, '').replace(/\s*$/, '').split(" "), // Remove any leading whitespace, then split on spaces.
resMatch = parseFloat( source[1], 10 ); // Parse out the resolution for each source in `srcset`.

if( screenRes >= resMatch && picImg.getAttribute( "src" ) !== source[0] ) {
var newImg = document.createElement("img");
if( screenRes >= resMatch ) {
if( picImg.getAttribute( "src" ) !== source[0] ) {
var newImg = document.createElement("img");

newImg.src = source[0];
// When the image is loaded, set a width equal to that of the original’s intrinsic width divided by the screen resolution:
newImg.onload = function() {
this.width = ( this.cloneNode( true ).width / resMatch ); // Clone the original image into memory so the width is unaffected by page styles
newImg.src = source[0];
// When the image is loaded, set a width equal to that of the original’s intrinsic width divided by the screen resolution:
newImg.onload = function() {
// Clone the original image into memory so the width is unaffected by page styles:
this.width = ( this.cloneNode( true ).width / resMatch );
}
picImg.parentNode.replaceChild( newImg, picImg );
}
break; // We’ve matched, so bail out of the loop here.
}
picImg.parentNode.replaceChild( newImg, picImg );
break;
}
}
} else {
// No `srcset` in play, so just use the `src` value:
picImg.src = match.getAttribute( "src" );
Expand Down

0 comments on commit d275823

Please sign in to comment.