Skip to content

Instantly share code, notes, and snippets.

@philcon93
Created February 9, 2017 06:56
Show Gist options
  • Save philcon93/87fa92b8dca3f99213157cc43c3c65fa to your computer and use it in GitHub Desktop.
Save philcon93/87fa92b8dca3f99213157cc43c3c65fa to your computer and use it in GitHub Desktop.
Simple Image Lazy Load and Fade
<!-- Give it a fade -->
<style type="text/css">
.thumbnail img{opacity: 1;transition: opacity 0.3s;}
.thumbnail img[data-src]{opacity: 0;}
</style>
<!-- Get that JS working -->
[%site_value id:'footer_javascript'%]
<script type="text/javascript" language="javascript">
$(document).ready(function(){
[].forEach.call(document.querySelectorAll('img[data-src]'), function(img) {
img.setAttribute('src', img.getAttribute('data-src'));
img.onload = function() {
img.removeAttribute('data-src');
};
});
});
</script>
[%/site_value%]
<!-- The image -->
<img data-src="[%asset_url type:'product' thumb:'thumb' id:'[@SKU@]'%][%param default%][%cdn_asset html:'0' library:'images'%]default_product.gif[%/cdn_asset%][%end param%][%/asset_url%]" class="product-image" alt="[@model@]" rel="itmimg[@SKU@]">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment