当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 图片预载入

Javascript
将HTML自动转为JS代码
一端时间轮换的广告
制作特殊字的脚本
美化下拉列表
文字幻灯片
判断是否输入完毕再激活提交按钮
JS控制表格隔行变色
左右两侧的广告代码 简单
父窗口获取弹出子窗口文本框的值
网页的标准,IMG不支持onload标签怎么办
一个很简单的办法实现TD的加亮效果.
文本框栏目介绍
自动检查并替换文本框内的字符
会自动逐行上升的文本框
列表内容的选择
对textarea框的代码调试,而且功能上使用非常方便,酷
提高代码性能技巧谈—以创建千行表格为例
Javascript的IE和Firefox兼容性汇编
让插入到 innerHTML 中的 script 跑起来
DEFER怎么用?

Javascript 中的 图片预载入


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 123 ::
收藏到网摘: n/a

特点:
1.图片预载入,载入后再显示。意图一次呈现,不会让一块一块下载破坏你的页面,绝佳的用户体验,颠覆传统的浏览器呈现图片的处理方式(需要后续函数支持)。
2.不会因载入图片造成脚本暂停假死,完全另一线程进行。不影响主程序流程。
3.提供及时的反馈,包括两方面的内容:1.正在载入什么图片 2.当前的百分数进度。大大提高留住用户眼球的概率,不会让用户因为苦等而离开。
4.容错支持,即使某个图片没有成功下载,也可以设置超时时间以便处理下一个图片。
5.多变的参数类型,尽最大可能方便使用。
复制代码 代码如下:

// save this as "image_loader.js"
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-//
/*
ImageLoader, Version 1.1, JavaScript
(c) 2006 Christian An <[email protected]>
With copyright not modified, you can use this program freely.
*/
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-//
function ImageLoader(className,Options){
this._ImageLoadStack = null;
this._currrentLoading = "";
this._FinalRun = false;
this.numLoaded = 0;
this.ClassName = className;
if(typeof(Options)=="undefined") Options = {};
if(isNaN(Options.Timeout) || Options.Timeout < 0 || Options.Timeout >100000){
this.EnableTimeout = false;
}else {
this.EnableTimeout = true;
this.Timeout = Options.Timeout;
}
if(typeof(Options.func)=="undefined"){
this.AfterFunction = null;
}else{
this.AfterFunction = Options.func;
}
if(typeof(Options.display)=="undefined"){
this.disDiv = null;
}else if(typeof(Options.display)=="string"){
this.disDiv = document.getElementById(Options.display);
}else if(typeof(Options.display)=="object"){
this.disDiv = Options.display;
}else{
this.disDiv = null;
}
if(typeof(Options.process)=="undefined"){
this.procDiv = null;
}else if(typeof(Options.process)=="string"){
this.procDiv = document.getElementById(Options.process);
}else if(typeof(Options.process)=="object"){
this.procDiv = Options.process;
}else{
this.procDiv = null;
}

if(typeof(document.imageArray)=="undefined") document.imageArray = new Array();
this.Load = function(){
var args = this.Load.arguments;
if(args.length!=0){
this._ImageLoadStack = new Array();
for(i=0; i<args.length; i++){
if(args[i].indexOf("#")!=0){
this._ImageLoadStack.push(args[i]);
}
}
}else if(this._ImageLoadStack == null){
this._runFinal();
}
this.numTotal = this._ImageLoadStack.length;
this._LoadAImage();
}
this._LoadAImage = function(){
if(this._ImageLoadStack.length!=0){
var sURL = this._ImageLoadStack.shift();
if(this.disDiv!=null) this.disDiv.innerHTML = sURL;
_currrentLoading = sURL;

var j = document.imageArray.length;
document.imageArray[j] = document.createElement("IMG");
document.imageArray[j].Owner = this;
document.imageArray[j].onload = function(){
this.Owner._LoadAImage();
this.onload = null;
}
document.imageArray[j].onerror = function(){
this.Owner._LoadAImage();
this.onload = null;
}
if(this.EnableTimeout){
window.setTimeout("if(_currrentLoading==\""+sURL+"\"){"+this.ClassName+"._LoadAImage()}",this.Timeout);
}
document.imageArray[j++].src = sURL;
if(this.procDiv!=null){
this.numLoaded++;
var percentage = Math.floor(this.numLoaded * 100 / this.numTotal);
this.procDiv.innerHTML = percentage;
}
}else{
this._runFinal();
}
}
this._runFinal = function(){
if(this._FinalRun == false){
this._FinalRun = true;
if(typeof(this.AfterFunction)=="function"){
this.AfterFunction();
}else if(typeof(this.AfterFunction)=="string"){
if (window.execScript){
window.execScript(this.AfterFunction);
}else{
window.eval(this.AfterFunction);
}
}
}
}
this.setLoadImages = function(imageArray){
if(typeof(imageArray)!="object") return;
this._ImageLoadStack = imageArray;
}
}

使用:
复制代码 代码如下:

var loader = new ImageLoader(ClassName,Options);

的形式创建该对象。
其中:
loader : 为 JavaScript 变量名;
ClassName : String 型: 为 loader 在 JavaScript 中的表达。 如果是在任何函数之外创建该对象,请直接赋以该变量的字符串形式,如对应loader 为"loader" ; 如果是某个函数体内,仍然赋以该变量的字符串形式,但是创建的变量名请使用 window.loader 的形式。
Options : Object 型,下列属性是支持的:
Timeout : Integer,可选。取值为1-100000,单位毫秒。非正整数表示不采用。此为一个图片的最大载入时间,如果提供这个参数,则某个图片不能正常下载时,可以跳过继续下载另一个图片。否则将一直等到该图片下载完成为止。
func : Function / String,必须。当所有图片载入之后调用的函数,通常是一个显示这些图片功能的函数。如果不提供这个函数,则整个机制将变得毫无作用。 Function型的参数会直接调用,String型的参数会当作JavaScript 语句来运行。
display :String / Object,可选。此为显示当前载入图片的DOM对象,该对象应该支持innerHTML属性。 当提供此参数为String 时,会被当作DOM对象的 id 来处理,若 Object 型,则直接当作一个DOM对象。提供其他类型没有效果。
process :String / Object,可选。此为以百分数显示当前载入进度的DOM对象,该对象应该支持innerHTML属性。 当提供此参数为String 时,会被当作DOM对象的 id 来处理,若 Object 型,则直接当作一个DOM对象。提供其他类型没有效果。
见下列示范:
复制代码 代码如下:

//在所有函数之外时
//function final(){...};
function $(par){
return document.getElementById(par)
}
var MyLoader = new ImageLoader("MyLoader ",{Timeout:1000,func: final,display:"display",process:$("process")});

//在某个函数体内时
function somefunc(){
//...
window.MyLoader = new ImageLoader("MyLoader ",{Timeout:1000,func: "alert('fine')",display:"display",process:$("process")});
//...
}

方法定义:
Load(paralist) :载入一系列图片。完毕后自动调用 func属性的内容。 paralist,可以是一些字符串的集合(但不要提供一个数组),各项由 , 隔开。这些字符串应该是图片的url。也可以不提供任何参数。Load方法将载入预先设定好的系列图片。如果没有预先设定过,则直接调用 func 属性的内容。若func没有提供,则没有任何效果。
复制代码 代码如下:

//sample:
MyLoader.Load("http://bbs.blueidea.com/images/blue/logo.gif",
"http://gg.blueidea.com/2006/chinaok/208x32.gif",
"http://gg.blueidea.com/2006/now/208x32.gif",
"http://gg.blueidea.com/2006/gongyi/banner.jpg",
"http://gg.blueidea.com/2006/flash8/pepsi.gif",
"http://www.google.com/intl/zh-CN_ALL/images/logo.gif");
//or if pic series is provided.
MyLoader.Load();

setLoadImages(ArrayImages): 设定要载入的图片系列。ArrayImages 应以数组的形式提供,数组的每一个元素都应当是一个图片的URL。不接受其他类型的参数。此方法调用后并不开始载入图片。
复制代码 代码如下:

//sample:
MyLoader.setLoadImages(["http://bbs.blueidea.com/images/blue/logo.gif",
"http://gg.blueidea.com/2006/chinaok/208x32.gif",
"http://gg.blueidea.com/2006/now/208x32.gif",
"http://gg.blueidea.com/2006/gongyi/banner.jpg",
"http://gg.blueidea.com/2006/flash8/pepsi.gif",
"http://www.google.com/intl/zh-CN_ALL/images/logo.gif"])

点击运行可以看到效果:
[Ctrl+A 全选 提示:你可先修改部分代码,再按运行]