Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@CodeMyUI
Created February 8, 2017 04:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CodeMyUI/b35463c623a50be609a1d0c89dd79906 to your computer and use it in GitHub Desktop.
Save CodeMyUI/b35463c623a50be609a1d0c89dd79906 to your computer and use it in GitHub Desktop.
Dominant Color Lazy Loading
.background(data-image="WpdHCafYSWjaguKSxPOjdbT1a/H0MfAAAAEElEQVQI12NgYGRiYGZhBQAALgAQ2bAGGQ")
img.lazy(src="https://lh3.googleusercontent.com/-6UtNFOzr6Rk/V2pO8a32QNI/AAAAAAAAHXE/tc8SACAEqWo_IM0N85rUHlc-ThVs65LaQ/w2048-h1280/sierra-2.jpg" )
.background(data-image="WeXlGlt8u7wc40QUtIYHNRR0wrG7TZAAAAEElEQVQI12NgYGRiYGZhBQAALgAQ2bAGGQ")
img.lazy(src="http://ihdwallpapers.com/walls/2015/10/12/os_x_el_capitan_desktop-wide.jpg" )
.background( data-image="X2xZnCkYJ4YXKqj3xTUVpKT15VjEr+AAAAEElEQVQI12NgYGRiYGZhBQAALgAQ2bAGGQ")
img.lazy(src="https://www.hdwallpapers.net/wallpapers/yosemite-wallpaper-for-2880x1800-60-798.jpg")
$('.background').each(function() {
var $b64 = $(this).data("image");
$(this).css("background", "url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAMAAACqqpYoAAAAElBMVE" + $b64 + "AAAABJRU5ErkJggg==) center/cover no-repeat");
});
$('.lazy').each(function() {
var $object = $(this),
$parent = $object.parent();
$object.on('load', function() {
$object.addClass('loaded');
$parent.removeAttr("data-image")
setTimeout(function() {
$parent.removeAttr("style");
}, 2000);
});
});
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
.background {
$size: 80vmin;
width: $size;
height: calc(#{$size} / 14 * 9);
margin: 56px auto;
box-shadow: 0 10px 75px -16px #000;
img.lazy{
height: 100%;
width: 100%;
opacity: 0;
transition: 1s;
&.loaded {
opacity: 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment