Skip to content
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

Open
jeffkreska opened this issue May 24, 2016 · 5 comments

Comments

@jeffkreska
Copy link

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.

@ddouble
Copy link

ddouble commented Oct 28, 2016

+1

@aFarkas
Copy link
Owner

aFarkas commented Oct 28, 2016

You can easily do this with a plugin.

@jeffkreska
Copy link
Author

Can you provide an example of the plugin you are talking about.

@aFarkas
Copy link
Owner

aFarkas commented Oct 28, 2016

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;
});

@bruncanepa
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants