Created
August 12, 2016 20:25
-
-
Save iamravenous/d3c0a51ff3e42c45c4983ee7fbb1ed14 to your computer and use it in GitHub Desktop.
Hide images and place as background-images
This file contains hidden or 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
/* | |
* 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