图片预载入

2016-02-19 12:02 2 1 收藏

下面这个图片预载入教程由图老师小编精心推荐选出,过程简单易学超容易上手,喜欢就要赶紧get起来哦!

【 tulaoshi.com - Web开发 】

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

// save this as "image_loader.js"

//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-//
/*
  ImageLoader, Version 1.1, JavaScript 
  (c) 2006 Christian An anchangsi@gmail.com

  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; iargs.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"])



来源:https://www.tulaoshi.com/n/20160219/1599578.html

延伸阅读
<HTML <HEAD<TITLE EvE HTML </TITLE <meta http-equiv="Content-Type" content="text/html; charset=gb2312" / <STYLE TYPE="text/css" TITLE=""  td{font-size: 12px;} </STYLE </HEAD <Body bgcolor="#E8EEED" background="" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" <DIV id=e...
胎膜早破怎么防预? 胎膜早破是指在临产前胎膜自然破裂。孕龄<37孕周的胎膜早破又称为早产(未足月)。胎膜早破胎膜早破是围生期最常见的并发症,可以对孕产妇、胎儿和新生儿造成严重不良后果。胎膜早破可导致早产率升高,围生儿病死率增加,宫内感染率及产褥感染率均升高。胎膜早破的原因有:创伤,宫颈内口松弛,生殖道病原微生物上行性...
一般大家的冬帽都是在实体店或者网上购买的,有些喜欢手工的人可能会自己用针线来做,但其实毛线帽可以用快递盒编织,是不是很神奇?整个制作的过程中比较难的部分就是编织部分了,但是也只要有耐心这一部分也是可以做好的,在剪裁快递盒的时候大家要注意不要太快伤到自己,利用铁丝的时候也要注意不要伤到手,快点来跟着图老师给的步骤做吧。 ...
标签: PHP
这是在看太平洋网的评论时看到的,太平洋网是用jsp做为后台语言,用来产生xml文件.然后在把数据绑定到html上的.我就用php也做了一个以下是源文件. -----------------xml.htm------------------ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd&qu...
标签: 绝地求生
绝地求生大逃杀怎么压枪预瞄 与大多数射击游戏一样,枪法很重要,但枪法不一定吃鸡。主要看你策略和对危险的感知嗅觉 无论怎么打,都是要开枪的。老想着活下去不进行火拼怎么提高技术。 1.开火模式 游戏里面按B可以切换开火模式: 自动、连发模式:在即将进入地形复杂的城区、房屋、密林之前,养成切换全自动...

经验教程

722

收藏

11
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部