Last active
August 29, 2015 14:16
-
-
Save Shipow/caf5fea2eea519f319e2 to your computer and use it in GitHub Desktop.
Angular Lazy load images with cache detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | |
} | |
}; | |
}]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
works with bootstrap class