Example of using the jQuery lazy load plugin jquery.lazyload.js instructions
jQuery lazy loading plugin jquery.lazyload.js is a plugin used to delay the loading of images. When the page scrolls to a specific position, the images will then load, effectively improving page loading speed and user experience. Below is an example of how to use it:
First, include the source files of the jQuery library and the jquery.lazyload.js plugin in the HTML page.
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="jquery.lazyload.js"></script>
Next, add the “lazyload” class and the “data-src” attribute to the images that need to be loaded lazily, with the “data-src” attribute specifying the actual address of the image.
<img class="lazyload" data-src="image.jpg" alt="Image">
Next, use the jQuery ready method to initialize the lazy loading plugin.
$(document).ready(function() {
$("img.lazyload").lazyload();
});
Finally, set a default placeholder image in CSS to be displayed before the actual image loads.
.lazyload {
background: url(placeholder.jpg) no-repeat center center;
}
In this way, when the page scrolls to the position of the image, the plugin will automatically load the image, replacing the placeholder.
It should be noted that the lazy loading plugin also has some optional configuration options that can be adjusted according to your needs. For example, you can control the amount of preloading of images by setting the threshold.
$("img.lazyload").lazyload({
threshold: 200 // 提前200像素加载图片
});
Furthermore, the plugin also offers various event callback functions that allow for custom operations to be executed before, during, and after loading. For detailed configuration and usage instructions, please refer to the official documentation of jquery.lazyload.js.