Skip to content

Instantly share code, notes, and snippets.

@Shipow
Last active August 29, 2015 14:16
Show Gist options
  • Save Shipow/caf5fea2eea519f319e2 to your computer and use it in GitHub Desktop.
Save Shipow/caf5fea2eea519f319e2 to your computer and use it in GitHub Desktop.
Angular Lazy load images with cache detection
app.directive('imgPreload', ['$timeout', function($timeout) {
return {
restrict: 'A',
scope: {
ngSrc: '@'
},
link: function(scope, element, attrs) {
var loaded = false;
element.on('load', function() {
loaded = true;
element.addClass('in');
}).on('error', function() {
//
});
$timeout(function() {
if (!loaded) {
element.addClass('fade')
}
}, 10); // is 10ms to detect the image was in the cache?
}
};
}])
@Shipow
Copy link
Author

Shipow commented Mar 8, 2015

works with bootstrap class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment