How to use CSS, JavaScript, and Ajax to implement picture preloading

Source: Internet
Author: User
Tags cdata tld

preloading A picture is a great way to improve the user experience. The images are preloaded in the browser, and visitors can surf the site smoothly and enjoy extremely fast loading speeds. This is good for a large proportion of the image galleries and images, which ensures that the images are released quickly and seamlessly, and help users get a better user experience when browsing the content of your site. This article will shareUsing CSS,JavaScriptandAjaxto implement a picture preloadedthree different technologies to enhance the performance and usability of your website. Let's take a look and hope to learn the web for youFront EndHelpful.method One: withCssand theJavaScriptTo implement pre-loadingThere are many ways to implement preloaded images, including usingCss,JavaScriptand the various combinations of the two. These technologies can be designed according to different design scenarios corresponding solutions, very efficient. Simple to useCss, it is easy and efficient to preload images with the following code: #preload-01 {background: URL (image-01.png) no-repeat-9999px-9999px; } #preload-02 {background: URL (image-02.png) no-repeat-9999px-9999px; } #preload-03 {background: URL (image-03.png) no-repeat-9999px-9999px; }put these threeIdselector applies to(X) HTMLelement, we can then pass theCSSof thebackgroundproperty to preload the picture onto a background outside the screen. As long as the paths of these pictures remain unchanged when they are inWebwhen other parts of the page are called, the browser uses the preloaded (cached) picture during the rendering process. Simple and efficient, no need for anyJavaScript. Although this method is efficient, there is still room for improvement. Images loaded with this method are loaded with other contents of the page, increasing the overall load time of the page. To solve this problem, we have added someJavaScriptcode to postpone the preload until the page has finished loading. The code is as follows: functionPreloader() { if(document.getElementById) { document.getElementById ("P1"). Style.background = "url (image-01.png) no-repeat"; document.getElementById ("P2"). Style.background = "url (image-02.png) no-repeat"; document.getElementById ("P3"). Style.background = "url (image-03.png) no-repeat"; } }functionaddloadevent(func) { varOldonload = window.onload; if(typeofWindow.onload! = ' function ') { Window.onload = func; }Else{ Window.onload =function() { if(Oldonload) { Oldonload (); } Func (); } } } Addloadevent (Preloader);in the first part of the script, we get the elements that use the class selector and set theBackgroundproperty to preload a different picture. the second part of the script, we useAddloadevent ()function to delayPreloader ()the load time of the function until the page has finished loading. ifJavascriptWhat happens when I can't run properly in the user's browser? Very simple, the picture will not be preloaded, when the page calls the picture, the normal display can. method Two: Use onlyJavascriptTo implement pre-loadingThe above method is sometimes very efficient, but we gradually find that it takes too much time in the actual implementation process. Instead, I prefer to use pureJavascriptto implement the pre-loading of the picture. Here are two of these preload methods, which can work beautifully on all modern browsers. JavascriptCode Snippet1 <Divclass= "hidden" > <ScriptType= "Text/javascript" > </!]] ></! [cdata[> Script>div>simply edit and load the path and name of the desired picture, which is easy to implement:This method is especially useful for preloading a large number of images. My gallery site uses this technology to preload images up to50more than one. Apply the script to the login page, and most gallery images will be preloaded as long as the user enters the login account. JavascriptCode Snippet2 <Divclass= "hidden" > <ScriptType= "Text/javascript" > </!]] ></! [cdata[> Script>div>This method is similar to the method above, and can preload any number of images. Add the following script to anyWebpage, you can edit it according to the program instructions. as you can see, each loading of a picture requires creating a variable, such as"IMG1 = new Image ();", and a picture source address declaration, such as"IMG3.SRC =". /path/to/image-003.gif ";". With this mode, you can load as many images as you need. We have also made improvements to this approach. Wrap the script into a function and use theAddloadevent(), delaying the preload time until the page has finished loading. functionPreloader() { if(document.images) { varIMG1 =NewImage (); varImg2 =NewImage (); varIMG3 =NewImage (); IMG1.SRC = "Http://domain.tld/path/to/image-001.gif"; IMG2.SRC = "Http://domain.tld/path/to/image-002.gif"; IMG3.SRC = "Http://domain.tld/path/to/image-003.gif"; } }function addloadevent(func) { varOldonload = window.onload; if(typeofWindow.onload! = ' function ') { Window.onload = func; }Else{ Window.onload =function() { if(Oldonload) { Oldonload (); } Func (); } } } Addloadevent (Preloader);method Three: UseAjaxTo implement pre-loadingThe method given above doesn't seem cool enough, so now let's look at a useAjaxa way to implement a picture preload. The method usesDOM, not just preloaded pictures, but also preloadedCSS,JavaScriptand other related things. UseAjax, than directly usingJavaScript, the advantage is thatJavaScriptand theCSSload does not affect the current page. The method is simple and efficient. Window.onload =function() { SetTimeout (function() { XHR to request a JS and a CSS varXHR =NewXMLHttpRequest (); Xhr.open (' GET ', ' http://domain.tld/preload.js '); Xhr.send ("); XHR =NewXMLHttpRequest (); Xhr.open (' GET ', ' http://domain.tld/preload.css '); Xhr.send ("); Preload image NewImage (). src = "http://domain.tld/preload.png"; }, 1000); };The above code pre-loads the"Preload.js","Preload.css"and the"Preload.png". +milliseconds are timed out to prevent the script from suspending, causing a functional problem on the normal page. below, let's see how to useJavascriptto implement the loading process: Window.onload =function() { SetTimeout (function() { Reference to varHead = document.getElementsByTagName (' head ') [0]; A new CSS varCSS = document.createelement (' link '); Css.type = "Text/css"; Css.rel = "stylesheet"; Css.href = "Http://domain.tld/preload.css"; A new JS varJS = document.createelement ("script"); Js.type = "Text/javascript"; JS.SRC = "Http://domain.tld/preload.js"; Preload JS and CSS Head.appendchild (CSS); Head.appendchild (JS); Preload image NewImage (). src = "http://domain.tld/preload.png"; }, 1000); };here, we passDomcreate three elements for preloading three files. As mentioned above, the use ofAjax, the load file is not applied to the load page. From this point of view,Ajaxmethod is superior toJavaScript. Source:Blog Park

How to use CSS, JavaScript, and Ajax to implement picture preloading

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.