Skip to content

Instantly share code, notes, and snippets.

@jacobarriola
Created October 31, 2016 15:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobarriola/5d9be370cb53513faeb0c145cf679ff3 to your computer and use it in GitHub Desktop.
Save jacobarriola/5d9be370cb53513faeb0c145cf679ff3 to your computer and use it in GitHub Desktop.
Blurry to awesome images
<figure class="placeholder" data-large="{path to large image}">
<img src="{path to small image}" class="img-small loaded" alt="{alt}" /> <!-- small image about 50px wide and height to match ratio; keep to less than 3k -->
<div style="padding-bottom: {image ratio}%;"></div> <!-- ratio is height-in-px/width-in-px * 100 -->
</figure>
jQuery(document).ready(function($) {
$('.placeholder').each(function(){
var imgLarge = new Image();
imgLarge.src = $(this).data('large');
imgLarge.onload = function () {
imgLarge.classList.add('loaded');
};
$(this).append(imgLarge);
});
return true;
});
.placeholder {
background-size: cover;
background-repeat: no-repeat;
position: relative;
overflow: hidden;
}
.placeholder img {
position: absolute;
opacity: 0;
top: 0;
left: 0;
width: 100%;
transition: opacity 1s linear;
}
.placeholder img.loaded {
opacity: 1;
}
.img-small {
filter: blur(50px);
/* this is needed so Safari keeps sharp edges */
transform: scale(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment