预载图片? [英] Preload Images?

查看:103
本文介绍了预载图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究使用JQuery预加载图像并遇到这个使用jQuery预加载图像

I have been looking into Preloading images with JQuery and came across this Preloading images with jQuery

现在可以有人告诉我,我是否必须以任何方式调用预装的图像?我假设这创建了一个img,然后浏览器缓存了它(至少对于那个页面)我只是通过它的URI使用预加载的图像?

Now can someone tell me, do I have to call the preloaded images in any way? I assume this creates a img and then the browser has it cached (at least for that page) and I just use the preloaded image by it's URI?

有人可以澄清这里?

推荐答案

预装图像可以通过一行简单的JavaScript来完成:

Preloading an image can be done with a simple line of JavaScript:

new Image().src='image.png';

要预加载JavaScript文件,请使用JavaScript include_DOM 技术并创建一个新文件

For preloading JavaScript files, use the JavaScript include_DOM technique and create a new

< script> ; 标记,如下所示:

<script> tag, like so:

var js = document.createElement('script'); js.src = 'mysftuff.js'; 
document.getElementsByTagName('head')[0].appendChild(js);

以下是CSS版本:

var css  = document.createElement('link');
css.href = 'mystyle.css';
css.rel  = 'stylesheet';
document.getElementsByTagName('head')[0].appendChild(css);

在第一个示例中,图像是请求但从未使用过,因此它不会影响当前图像页。在第二个示例中,脚本被添加到页面中,因此在下载时,它将被解析并执行。 CSS也是如此 - 它也将应用于页面。如果这是不合需要的,您仍然可以使用XMLHttpRequest预加载资产。

In the first example, the image is requested but never used, so it doesn’t affect the current page. In the second example, the script is added to the page, so as well as being downloaded, it will be parsed and executed. The same goes for the CSS — it, too, will be applied to the page. If this is undesirable, you can still pre-load the assets using XMLHttpRequest.

有关完整教程,使您的网站超高速请访问我从许多网站和博客中挑选的以下链接

For complete tutorial on, "making your website superfast" please visit the following link which i hand picked from many websites and blogs

使用jQuery预加载图像

这篇关于预载图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆