Let's talk about the JavaScript image pre-loading technology.

Source: Internet
Author: User

BKJIA recommended topics: Answers to questions about JavaScript learning notes

A typical example of getting the image size through pre-loading:

 
 
  1. var imgLoad = function (url, callback) {  
  2. var img = new Image();  
  3.  
  4. img.src = url;  
  5. if (img.complete) {  
  6. callback(img.width, img.height);  
  7. } else {  
  8. img.onload = function () {  
  9. callback(img.width, img.height);  
  10. img.onload = null;  
  11. };  
  12. };  
  13.  
  14. }; 

We can see that the onload method must wait until the image is loaded, and the speed is not flattering.

Different from desktop applications, web applications provide the best response speed. If you want to achieve both speed and elegance, you must obtain the image size in advance. How can you obtain the image size before loading the image?

More than a decade of online experience tells me that when loading images in a browser, you will see that the images will take up a lot of space before loading them, in addition, most of the images here do not have the default width and height attributes, because the browser can obtain the image header data. Based on this, you only need to use javascript to regularly detect the size status of the image to know that the image size is ready.

Implementation Code:

 
 
  1. Var imgReady = function (url, callback, error ){
  2. Var width, height, intervalId, check, div,
  3. Img = new Image (),
  4. Body = document. body;
  5.  
  6. Img. src = url;
  7.  
  8. // Read from the cache
  9. If (img. complete ){
  10. Return callback (img. width, img. height );
  11. };
  12.  
  13. // Obtain the image header data by placeholder in advance
  14. If (body ){
  15. Div = document. createElement ('div ');
  16. Div.style.css Text = 'visibility: hidden; position: absolute; left: 0; top: 0; width: 1px; height: 1px; overflow: Den den ';
  17. Div. appendChild (img)
  18. Body. appendChild (div );
  19. Width = img. offsetWidth;
  20. Height = img. offsetHeight;
  21.  
  22. Check = function (){
  23. If (img. offsetWidth! = Width | img. offsetHeight! = Height ){
  24. ClearInterval (intervalId );
  25. Callback (img. offsetWidth, img. clientHeight );
  26. Img. onload = null;
  27. Div. innerHTML = '';
  28. Div. parentNode. removeChild (div );
  29. };
  30. };
  31.  
  32. IntervalId = setInterval (check, 150 );
  33. };
  34.  
  35. // Obtain it after loading
  36. Img. onload = function (){
  37. Callback (img. width, img. height );
  38. Img. onload = img. onerror = null;
  39. ClearInterval (intervalId );
  40. Body & img. parentNode. removeChild (img );
  41. };
  42.  
  43. // Image loading error
  44. Img. onerror = function (){
  45. Error & error ();
  46. ClearInterval (intervalId );
  47. Body & img. parentNode. removeChild (img );
  48. };
  49.  
  50. };

Is it easy? In this way, the speed of obtaining photography-level photos is usually dozens of times higher than that of onload. However, for normal web (within 800 × 600) browsing-level images, the effect of flash can be achieved.

Well, please watch the pleasant DEMO. Tested browsers: Chrome, Firefox, Safari, Opera, IE6, IE7, and IE8.
 

Original link: http://www.planeart.cn /? P = 1121

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.