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 |
Share toggles | 3,574 Kb |
A Pen by Rosh Jutherford | 4,410 Kb |
Pre-loader 1 by Chris Hall | 2,658 Kb |
Twisted | 3,216 Kb |
Icons | 2,328 Kb |
RvV 2 | 3,115 Kb |
Rubee | 3,195 Kb |
Flat Range Sliders | 2,467 Kb |
Beautiful Login Form | 2,861 Kb |
Responsive CSS Menu | 2,559 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 |
Scroll to view if element partially out of view port height | ChrisMaki | 2,104 Kb |
Portfolio Landing Page | FDfranklin | 3,585 Kb |
Haml Calendar | Katydecorah | 5,643 Kb |
Test | Dviate | 2,668 Kb |
A Pen by Jess | Jessamyne | 5,100 Kb |
Simple jQuery Slider | Jurbank | 2,874 Kb |
P1 | Vivi_Lai | 1,533 Kb |
Two tables and header with jspdf-autotable | Someatoms | 2,245 Kb |
Animated skewed panes | NyX | 4,462 Kb |
AngularJS Skills | Supro | 3,312 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!