jquery settings do not load ul

王林
Release: 2023-05-14 10:51:40
Original
626 people have browsed it

In web development, pictures and videos are indispensable elements of web pages, but sometimes due to network reasons, the loading speed of these elements will be very slow, causing the page to load slowly or even crash directly. In some cases, we may only need to display some pictures and videos, so how to set it up? This article will use jquery as an example to introduce how to set up not loading ul in order to speed up page loading.

1. Technical background

In front-end development, jquery is one of the most commonly used JS frameworks. It can help us quickly complete the development of web pages and improve development efficiency. In jquery, we can write code to not load the ul and improve the loading speed of the page.

2. Set the method of not loading ul

In jquery, we can write javascript code to achieve not loading ul. The specific method is as follows:

  1. Find the div where the ul tag is located, and add "lazyLoad" to the class of the div, for example:

    <div class="lazyLoad">

  2. Write the following code in jquery:

    $(window).on("load",function(){
      $(".lazyLoad").each(function(){
        var totalHeight = $(this).height();
        $(this).find("img").each(function(){
          totalHeight -= $(this).height();
          if(totalHeight > 0){
            $(this).attr("src",$(this).data("src"));
          }
        });
        $(this).addClass("loaded");
      });
    });
    Copy after login

    In the above code, we first use $(window).on("load",function(){}) to listen to the window loading event, Then iterate through each div containing the .lazyLoad class name. When traversing each div, we obtain the sum of the heights of all images, and then determine which images have the src attribute added by judging whether totalHeight is greater than 0. If totalHeight is greater than 0, add the image to the src attribute, otherwise the image will not be loaded. Finally, we add the loaded class name to the div.

  3. Add the following code in css:

    .lazyLoad img{
      opacity:0;
      transition:opacity 500ms ease-in-out;
    }
    .lazyLoad.loaded img{
      opacity:1;
    }
    Copy after login

    In the code, we set the opacity to 0 for .lazyLoad img and add css animation. After .lazyLoad loads all the images that need to be loaded, we will add the loaded class name to them. At this time, the opacity of .lazyLoad.img will be changed to 1, and all images with the src attribute will be displayed.

3. Effect demonstration

In order to better understand how to set up not loading ul, this article has prepared an effect demonstration for everyone. In the demo page, there are 10 pictures, but only 5 of them will be loaded, and the other 5 will not be loaded. Opening the console, we can see that only 5 images have the src attribute added, and the other 5 images have not been loaded.

4. Summary

This article introduces how to use jquery to set not to load ul, so as to speed up page loading. We use javascript code to traverse the div containing the .lazyLoad class name, find all the pictures that need to be loaded, determine whether the picture needs to be loaded by calculating the difference between the height of the picture and the total height, and add the loaded class name to complete the display effect.

In short, this is a very practical optimization technique and I hope it will be helpful to everyone’s front-end development work.

The above is the detailed content of jquery settings do not load ul. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template