Skip to content

Lazyload background-image of div elements #944

Answered by boxedpaul
boxedpaul asked this question in Q&A
Discussion options

You must be logged in to vote

(facepalm)

The image I was trying to load included a space. Because I hadn't enclosed the background-image:url in quotes, it was erroring.

Corrected code:

<script>
//add simple support for background images:
document.addEventListener('lazybeforeunveil', function(e){
var bg = e.target.getAttribute('data-bg');
if(bg){
e.target.setAttribute("style", "background-image: url('" + bg + "')");
}
});
</script>

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by boxedpaul
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant