Home > Article > Web Front-end > Use simple image preloading components to improve the user experience of html5 mobile pages _html5 tutorial skills
When working on h5 mobile pages, I believe you must have encountered a situation where the page has been opened, but the images inside have not been loaded. Although this problem does not affect the function of the page, it is not conducive to the user experience. Regardless of the network speed, there are many ways to solve this problem: the most basic is to optimize performance from aspects such as http request merging, cache management, image compression, etc.; the other is to pre-process all images used in the page. In the loading process, when the user opens the page, the first screen is not displayed immediately. Instead, the resource loading effect is displayed first, and then the main content of the page is displayed after the loading is completed. This can solve the problem. Although this loading effect takes up the user's browsing time, we can make it more beautiful and interesting, so it will not affect the user experience. This article implements this idea and provides a very simple image preloading component. It is simple to implement and not weak in function. It should be of reference value to you when making mobile pages.
Effect:
1. Implementation ideas
The img tag in HTML and background-imag in CSS will trigger the browser to load the related image. However, if the image has already been loaded, the browser will directly use the loaded image, thus It can be rendered on the page instantly. Through javascript, create Image objects, and then set the src attribute of these objects to the address of the image to be loaded, which can also trigger the browser to load the image. You can use this to realize the image preloading function: first use those related to the page. Hide the elements of the image, then use js to load the image, wait until all images are loaded, and then display the hidden elements. However, this is just a basic implementation idea. To complete a preloading component with a more robust function, there are still three problems:
1) Progress problem
Since preloading is done at the same time, a preloading component must also be done. The effect is that the loading progress needs to be notified to the external context in real time. There are two ways to implement progress. The first is the loaded data size/total data size, and the second is the number of loaded files/total number of files. In the browser, it is unrealistic to use the first method. , there is no native way to do it, so we can only use the second method.
2) The problem of image loading failure
For example, if there are 4 images, 50% of them have been loaded, and an error occurs when loading the third image. Should the progress be fed back to 75%? The answer is: yes. If this is not done, the progress will never reach 100%, and the main content of the page will have no chance to be displayed. Although the image loading may fail, it has nothing to do with the loader. Maybe the image itself does not exist? This means that image loading failure should not affect the functionality of the loader.
3) The problem of image loading timeout
The image cannot be loaded for too long, otherwise the user will stay in the loading effect and cannot see the main content, and the user's waiting time will be extended uncontrollably, resulting in a decline in user experience. This will It goes against the original intention of the loader. Therefore, a loading timeout should be set for each image. If the loading is not completed after the timeout of all images, the loading should be actively abandoned, the external context should be notified that the loading is complete, and the main content should be displayed.
Based on the above requirements, the implementation provided in this article is:
Usage (corresponding to test.html in the code):
Run result:
2. Demo description
The effect given at the beginning of this article, the corresponding page is index.html, there are two issues that need to be explained about this effect :
1) It uses the sliding screen idea introduced in the previous blog Hammer.js carousel principle to implement a simple sliding screen function, and wraps some of its logic in swipe.js, A global variable Swipe is provided to the outside world. This module has an init method so that the sliding screen related functions can be initialized externally by calling Swipe.init(). Originally, this init method was not provided. The sliding screen will be initialized after the js is loaded. Function, with this init method, the sliding screen logic can be delayed until the loading is completed to initialize. index.html references a total of 5 js:
其中imgLoader.js就是前面介绍图片加载器的实现,前三个js都是为最后一个swipe.js服务的,感兴趣的可以继续我的博客利用轮播原理结合hammer.js实现简洁的滑屏功能了解相关内容。不过滑屏不是本文的重点,不了解swipe.js不会影响理解本文的内容~
2)虽然我在demo中用到了3张比较大的图片,但是由于在本地环境,加载速度还是非常快,所以一开始的时候,很难看到预加载的效果,最后只能想办法在每个进度回调之前做一下延迟,这才可以看到前面gif图片一开始的那个loading效果,实现方式是:
在真实环境,最好还是不要刻意去加这种延迟,没必要为了让用户看到一个好看有趣的加载效果,就浪费它不必要的等待时间,所以真实环境还是应该用下面的代码:
3. Notes
Preloading is a relatively common implementation effect, but when using it, there are some issues that need to be paid attention to:
1) What Use
when the page is large. Generally, you should consider using it if the page size exceeds 3M. The page contains pictures with a relatively large amount of data. You can consider using it when the mobile phone test can clearly show that the loading is slow.
2) Try to use sprite images
3) When implementing the loading effect, try not to use images. Even if you want to use them, you should use very small images, otherwise the loading effect will be stuck there and it will be meaningless.
4. Summary
This article mainly introduces a simple image preloader, which can be applied to the development of h5 mobile pages. Under its ideas, If necessary, you can also modify it and use it to load other types of resources, such as audio or video files. After all, these types of DOM objects also provide properties and callbacks similar to Image objects. Contrary to the preloading method, there is also a technology of lazy loading of images. There are already relatively easy-to-use jquery plug-ins on the Internet, but it is still worth learning more about its ideas and implementation points. I will do it when I have time. Research research. At the same time, thank you all for your continued support of the Script House website!