Home > Web Front-end > JS Tutorial > Use CSS, JavaScript and Ajax to achieve efficient image preloading_javascript skills

Use CSS, JavaScript and Ajax to achieve efficient image preloading_javascript skills

WBOY
Release: 2016-05-16 17:19:44
Original
934 people have browsed it

Method 1: Use CSS and JavaScript to implement preloading

There are many ways to implement preloading images, including using CSS, JavaScript and various combinations of the two. These technologies can design corresponding solutions according to different design scenarios and are very efficient.
Using CSS alone, you can preload images easily and efficiently. The code is as follows:

Copy the code The code is as follows:

#preload-01 { background: url(http://domain.tld/image-01.png) no-repeat -9999px -9999px; }
#preload-02 { background: url (http://domain.tld/image-02.png) no-repeat -9999px -9999px; }
#preload-03 { background: url(http://domain.tld/image-03.png) no-repeat -9999px -9999px; }

Applying these three ID selectors to the (X)HTML element, we can preload the image off-screen through the background attribute of CSS on the background. As long as the paths to these images remain the same, the browser will use the preloaded (cached) images during the rendering process when they are called elsewhere on the web page. Simple, efficient and doesn't require any JavaScript.
While this method is efficient, there is still room for improvement. Images loaded using this method will be loaded together with other content on the page, increasing the overall loading time of the page. To solve this problem, we added some JavaScript code to delay the preloading time until the page is loaded. The code is as follows:
Copy code The code is as follows:

// better image preloading @ http://perishablepress.com/press/2009/12/ 28/3-ways-preload-images-css-javascript-ajax/
function preloader() {
if (document.getElementById) {
document.getElementById("preload-01 ").style.background = "url(http://domain.tld/image-01.png) no-repeat -9999px -9999px";
document.getElementById("preload-02").style.background = "url(http://domain.tld/image-02.png) no-repeat -9999px -9999px";
document.getElementById("preload-03").style.background = "url(http: //domain.tld/image-03.png) no-repeat -9999px -9999px";
}
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(preloader);

At the beginning of the script In one part, we get the element using the class selector and set the background attribute on it to preload different images.
In the second part of the script, we use the addLoadEvent() function to delay the loading time of the preloader() function until the page is loaded.
What happens if JavaScript doesn’t run properly in the user’s browser? It's very simple. The image will not be preloaded. When the page calls the image, it will be displayed normally.

Method 2: Only use JavaScript to implement preloading

The above method is sometimes very efficient, but we gradually found that it consumes too much time in the actual implementation process. Instead, I prefer to use pure JavaScript for image preloading. Below are two such preloading methods that work beautifully on all modern browsers.

JavaScript code snippet 1

You only need to simply edit and load the path and name of the required image. It is easy to implement:
Copy code The code is as follows:



This method is especially suitable for preloading a large number of images. My gallery website uses this technology to preload over 50 images. Apply this script to the login page and most of the gallery images will be preloaded as soon as the user enters their login ID.

JavaScript Code Snippet 2

This method is similar to the above method and can also preload any number of images. Add the following script to any Web page and edit it according to the program instructions.
Copy code The code is as follows:



As you can see, each time you load an image, you need to create a variable, such as "img1 = new Image();", and declare the image source address, such as "img3.src = "../path /to/image-003.gif";". Using this pattern, you can load as many images as you need.
We have improved this method again. Wrap this script into a function and use addLoadEvent() to delay the preloading time until the page is loaded.
Copy code The code is as follows:

function preloader() {
if (document. images) {
var img1 = new Image();
var img2 = new Image();
var img3 = new Image();
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) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload() ;
}
func();
}
}
}
addLoadEvent(preloader);

Method 3: Use Ajax to implement Preloading

The method given above does not seem cool enough, so now let’s look at a method of using Ajax to implement image preloading. This method uses DOM to not only preload images, but also preload CSS, JavaScript and other related things. The advantage of using Ajax over using JavaScript directly is that the loading of JavaScript and CSS will not affect the current page. The method is simple and efficient.
Copy code The code is as follows:

window.onload = function() {
setTimeout (function() {
// XHR to request a JS and a CSS
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://domain.tld/preload .js');
xhr.send('');
xhr = new XMLHttpRequest();
xhr.open('GET', 'http://domain.tld/preload.css' );
xhr.send('');
// preload image
new Image().src = "http://domain.tld/preload.png";
}, 1000 );
};

The above code preloads "preload.js", "preload.css" and "preload.png". The 1000 millisecond timeout is to prevent the script from hanging and causing functional issues with normal pages.
Next, let’s see how to use JavaScript to implement the loading process:
Copy the code The code is as follows:

window.onload = function() {
setTimeout(function() {
// reference to
var head = document.getElementsByTagName('head')[0] ;
// a new CSS
var css = document.createElement('link');
css.type = "text/css";
css.rel = "stylesheet";
css.href = "http://domain.tld/preload.css";
// a new JS
var js = 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
new Image().src = "http://domain.tld/preload.png";
}, 1000);
};

Here, we create three elements through DOM to preload three files. As mentioned above, with Ajax, loading files is not applied to the loading page. From this point of view, Ajax methods are superior to JavaScript.
Related labels:
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