Skip to content

Instantly share code, notes, and snippets.

@iamravenous
Created August 12, 2016 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamravenous/d3c0a51ff3e42c45c4983ee7fbb1ed14 to your computer and use it in GitHub Desktop.
Save iamravenous/d3c0a51ff3e42c45c4983ee7fbb1ed14 to your computer and use it in GitHub Desktop.
Hide images and place as background-images
/*
* Image as background
* Hide images and place as background-images
* Author: Franco Moya - @iamravenous
*/
document.addEventListener("DOMNodeInserted", function() {
var imgSrc = document.querySelectorAll("[data-img-src]");
for (var i = 0; i < imgSrc.length; i++) {
var imgUrl = imgSrc[i].getAttribute('src');
var imgParent = imgSrc[i].parentNode;
if (imgUrl && imgParent.hasAttribute('data-img-parent')) {
imgSrc[i].style.display = 'none';
imgParent.style.backgroundImage="url('" + imgUrl + "')";
}
}
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment