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,264 |
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 |
Twisted | 3,216 Kb |
Download Button | 4,308 Kb |
Apple iPhone 5s style scrolling | 4,740 Kb |
Share toggles | 3,574 Kb |
RvV 2 | 3,115 Kb |
Flame by Kenji Droullard | 3,359 Kb |
Flat Range Sliders | 2,467 Kb |
CSS Fine Art | 5,996 Kb |
Game-Style Header | 4,486 Kb |
Parallax Canvas Gallery | 12,954 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 |
Tic Tac Toe | Volv | 4,862 Kb |
Svg animation draw | SzymonDziewonski | 5,545 Kb |
SVG Animation | Pollardld | 3,133 Kb |
Pure CSS Tooltip | APinix | 2,815 Kb |
Fluid Grid 12 | Alexoliverwd | 2,309 Kb |
Navier Stoke Fluid Simulation | Esimov | 8,584 Kb |
Transition | Shayhowe | 1,632 Kb |
Wikipedia Viewer | Thomasvaeth | 2,549 Kb |
Simple Carousel Pure CSS | Dangvanthanh | 4,080 Kb |
Google Fonts Sass Mixin | HugoGiraudel | 4,237 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!