Last active
August 29, 2015 14:02
-
-
Save tjFogarty/ce385647f71a8f17b8a8 to your computer and use it in GitHub Desktop.
This doesn't include checks to see if this is supported on certain browsers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Detects using window.performance | |
* We can serve small images first, then upgrade on faster connections | |
* See: http://mattandrews.info/talks/port80-2013/#/ | |
*/ | |
(function () { | |
var perf = window.performance, | |
start = perf.timing.requestStart, // just before page is requested | |
end = perf.timing.responseStart, // first byte received | |
totalTime = end - start; | |
/** Responsible for upgrading images */ | |
var Performance = { | |
images: document.getElementsByTagName('img'), | |
timing: totalTime, | |
/** Kick things off */ | |
init: function () { | |
this.imageSize(); | |
}, | |
/** Loop through images, detecting if they have a 'data-large' attribute */ | |
imageSize: function () { | |
var largeSrc = null, | |
image = null; | |
for (var i = Performance.images.length - 1; i >= 0; i--) { | |
image = this.images[i]; | |
largeSrc = image.getAttribute('data-large'); | |
if (largeSrc !== null) { | |
image.src = largeSrc; | |
} | |
} | |
} | |
}; | |
if (Performance.timing < 1000) { | |
Performance.init(); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment