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 08 November 2021, Monday.

How do I make an lazy load for background images?
  1. Lazy Load for Background Images Previews
  2. Lazy Load for Background Images HTML Codes
  3. Lazy Load for Background Images CSS Codes
  4. Lazy Load for Background Images JS Codes
Lazy Load for Background Images Previews

Lazy Load for Background Images 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 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 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();
});
Do you want hide your ip address?Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup.