Lazy Load for Background Images

Size
2,977 Kb
Views
22,264

How do I make an lazy load for background images?

I was searching around for a lazy-load alternative that would work with background images. I didn't have any luck so I came up with this. May work on making it my first jQuery plugin :). What is a lazy load for background images? How do you make a lazy load for background images? This script and codes were developed by Rosh Jutherford on 04 September 2022, Sunday.

Lazy Load for Background Images Previews

Lazy Load for Background Images - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Lazy Load for Background Images</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <style> /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */ html { background: rgba(200,200,200,0.3); backface-visibility: hidden;
}
h1 { width: 100%; text-align: center; font-weight: 100; padding-top: 20px; color: rgba(200,200,200,1)
}
div { width: 300px; height: 300px; margin: 150px auto; position: relative;
}
div:after { content: ''; width: 100%; height: 100%; position: absolute; top: 0; left: 0; background: rgba(200,200,200,0.5); transition: opacity 0.5s ease;
}
.loaded { background: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_mrraktQTCS1st5lhmo1_1280.jpg"); background-position: center center; background-repeat: no-repeat; background-size: cover;
}
.loaded:nth-of-type(2){ background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_mufr0mmWYW1st5lhmo1_1280.jpg);
}
.loaded:nth-of-type(3){ background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_mr80snx79b1st5lhmo1_1280.jpg);
}
.loaded:nth-of-type(4){ background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_mrraktQTCS1st5lhmo1_1280.jpg);
}
.loaded:nth-of-type(5){ background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_msuei3sMTo1st5lhmo1_1280.jpg);
}
.loaded:nth-of-type(6){ background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_mtw7mhZsCe1st5lhmo1_1280.jpg);
}
.loaded:nth-of-type(7){ background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_msuei3sMTo1st5lhmo1_1280.jpg);
}
.loaded:nth-of-type(8){ background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_mufrlh9WqW1st5lhmo1_1280.jpg);
}
div.loaded:after { opacity: 0;
} </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body> <h1>Lazy Load for background Images</h1>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Lazy Load for Background Images - Script Codes CSS Codes

html { background: rgba(200,200,200,0.3); backface-visibility: hidden;
}
h1 { width: 100%; text-align: center; font-weight: 100; padding-top: 20px; color: rgba(200,200,200,1)
}
div { width: 300px; height: 300px; margin: 150px auto; position: relative;
}
div:after { content: ''; width: 100%; height: 100%; position: absolute; top: 0; left: 0; background: rgba(200,200,200,0.5); transition: opacity 0.5s ease;
}
.loaded { background: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_mrraktQTCS1st5lhmo1_1280.jpg"); background-position: center center; background-repeat: no-repeat; background-size: cover;
}
.loaded:nth-of-type(2){ background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_mufr0mmWYW1st5lhmo1_1280.jpg);
}
.loaded:nth-of-type(3){ background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_mr80snx79b1st5lhmo1_1280.jpg);
}
.loaded:nth-of-type(4){ background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_mrraktQTCS1st5lhmo1_1280.jpg);
}
.loaded:nth-of-type(5){ background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_msuei3sMTo1st5lhmo1_1280.jpg);
}
.loaded:nth-of-type(6){ background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_mtw7mhZsCe1st5lhmo1_1280.jpg);
}
.loaded:nth-of-type(7){ background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_msuei3sMTo1st5lhmo1_1280.jpg);
}
.loaded:nth-of-type(8){ background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/25381/tumblr_mufrlh9WqW1st5lhmo1_1280.jpg);
}
div.loaded:after { opacity: 0;
}

Lazy Load for Background Images - Script Codes JS Codes

var ll = $('div');
var lh = []
var wscroll = 0;
var wh = $(window).height();
function update_offsets(){ $('div').each(function(){ var x = $(this).offset().top; lh.push(x); });
};
function lazy() { wscroll = $(window).scrollTop(); for(i = 0; i < lh.length; i++){ if(lh[i] <= wscroll + (wh - 200)){ $('div').eq(i).addClass('loaded'); }; };
};
// Page Load
update_offsets();
lazy();
$(window).on('scroll',function(){ lazy();
});
Lazy Load for Background Images - Script Codes
Lazy Load for Background Images - Script Codes
Home Page Home
Developer Rosh Jutherford
Username the_ruther4d
Uploaded September 04, 2022
Rating 4
Size 2,977 Kb
Views 22,264
Do you need developer help for Lazy Load for Background Images?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Rosh Jutherford (the_ruther4d) Script Codes
Create amazing sales emails with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!