Lazy Load for Background Images
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 - 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();
});

Developer | Rosh Jutherford |
Username | the_ruther4d |
Uploaded | September 04, 2022 |
Rating | 4 |
Size | 2,977 Kb |
Views | 22,253 |
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!
Name | Size |
Kinetic Mark by Philip Thepkaysone | 2,504 Kb |
Finn by Meg Robichaud | 5,370 Kb |
Game-Style Header | 4,486 Kb |
CSS Fine Art | 5,996 Kb |
Hover effect demo | 2,749 Kb |
A Pen by Rosh Jutherford | 4,410 Kb |
Beautiful Login Form | 2,861 Kb |
Flame by Kenji Droullard | 3,359 Kb |
Download Button | 4,308 Kb |
Pre-loader 1 by Chris Hall | 2,658 Kb |
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!
Name | Username | Size |
CSS Gem Badge | Orchard | 3,335 Kb |
CSS only simple parallax scroll | Stanssongs | 3,708 Kb |
A Pen by utcwebdev | Utcwebdev | 2,856 Kb |
404 Page | Saransh | 2,666 Kb |
Flexbox slider | Rendro | 3,459 Kb |
WRENCH - STAFF | Lolita-adams | 1,608 Kb |
DNA Double Helix | Hugo | 5,112 Kb |
Console fun | Dviate | 1,500 Kb |
Fellowship of the Ring | Aussieyang | 2,639 Kb |
Sidebar Thing | Jonambas | 2,779 Kb |
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. Hide Your IP Now!