图片预加载 js css预加载
图片预加载, 效果非常明显, 特别是有多个图,
方法很简单 , 体验提升了不少
<div class="hidden">
<script type="text/javascript">
<!--//--><![CDATA[//><!--
var images = new Array()
function preload() {
for (i = 0; i < preload.arguments.length; i++) {
images[i] = new Image()
images[i].src = preload.arguments[i] }
}
preload(
"http://domain.tld/gallery/image-001.jpg",
"http://domain.tld/gallery/image-002.jpg",
"http://domain.tld/gallery/image-003.jpg"
)
//--><!]]>
</script>
</div>
将该脚本应用到登录页面,只要用户输入登录帐号,大部分画廊图片将被预加载。
面所给出的方法似乎不够酷,那现在来看一个使用Ajax实现图片预加载的方法。
该方法利用DOM,不仅仅预加载图片,还会预加载CSS、JavaScript等相关的东西。
使用Ajax,比直接使用JavaScript,优越之处在于JavaScript和CSS的加载不会影响到当前页面。该方法简洁、高效。
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);
};
上面代码预加载了“preload.js”、“preload.css”和“preload.png”。1000毫秒的超时是为了防止脚本挂起,而导致正常页面出现功能问题。
下面,我们看看如何用JavaScript来实现该加载过程:
window.onload = function() {
setTimeout(function() {
// reference to <head>
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);
};
这里,我们通过DOM创建三个元素来实现三个文件的预加载。
正如上面提到的那样,使用Ajax,加载文件不会应用到加载页面上。从这点上看,Ajax方法优越于JavaScript。
http://www.qdfuns.com/notes/36458/93b1e49cbfc3d30d568b414e242b5aa1.html
- iis 发布asp.net mvc 网站时候js css 压缩问题,图片不加载问题
- 动态加载并显式图片
- 直接加载在固定路径的图片(文件)到数据库中?
- 为对话框加载背景图片并在上面输出文本
- 判断网页或图片加载完毕否- -
- TImage控件加载JGP图片
- 图片数据的加载方案
- 加载外部图片及对加载图片的控制
- 使用ATL库中的CImage类加载JPG图片作为对话框背景
- 利用IPicture接口加载、显示图片
- 水晶报表中如何动态加载图片(图片文件版本及数据库版本)
- 利用IPicture接口加载、显示图片
- Panel1加载图片
- 实时显示图片加载进度
- 利用IPicture接口加载、显示图片
- 使用ATL库中的CImage类加载图片作为对话框背景
- 加载图片
- 利用IPicture接口加载、显示图片
- 图片加载进度实时显示
- 用js实现广告图片后加载