-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Retain the default image if the data-src fails to load (404) #269
Comments
+1 |
You can easily do this with a plugin. |
Can you provide an example of the plugin you are talking about. |
You can see the JS API documentation. Here is an untested example, that should work for your needs: document.addEventListener('lazybeforeunveil', function(e){
var img = e.target;
var src = img.getAttribute('data-src');
var newImage = document.createElement('img');
e.preventDefault();
img.classList.add('lazyloading');
img.classList.remove('lazyload');
newImage.onload = function(){
img.src = src;
img.classList.add('lazyloaded');
};
newImage.onerror = function(){
img.classList.add('lazyloaded');
};
newImage.src = src;
}); |
This should work: <img
{...props}
onError={e => {
e.target.src = 'defaultImage.png';
}}
> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I only want the data-src image to replace the src image if the data-src image is successfully loaded. If the data-src image returns a 404,503 etc error, then don't change the src attribute.
The text was updated successfully, but these errors were encountered: