Skip to content

Instantly share code, notes, and snippets.

@agustik
Created October 13, 2015 17:01
Show Gist options
  • Save agustik/cdd3760c6ef00892e9ae to your computer and use it in GitHub Desktop.
Save agustik/cdd3760c6ef00892e9ae to your computer and use it in GitHub Desktop.
Angularjs Lazyload images
/**
<div lazyimg lowres="small_landscape.jpg" highres="large_landscape.jpg" class="lazy-loader">
</div>
**/
angular.module('lazy', [])
.directive('lazyimg', function (){
return {
restrict: 'ACE',
template : function (element, attr){
return '<img class="low-resolution-image" src="' + attr.lowres + '"></img><img class="hidden high-resolution-image" src="' + attr.highres + '"></img>';
},
compile : function (element){
var lowres = element[0].getElementsByClassName('low-resolution-image');
var highres = element[0].getElementsByClassName('high-resolution-image');
angular.element(highres).bind('load', function (){
angular.element(lowres).addClass('hidden');
angular.element(highres).removeClass('hidden').unbind('load');
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment