图片预载入第1/3页

 更新时间:2006年10月03日 00:00:00   作者:  
特点:
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; 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;
    }

}

相关文章

  • layui中的tab控件点击切换触发事件

    layui中的tab控件点击切换触发事件

    这篇文章主要介绍了layui中的tab控件点击切换触发事件,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • javascript模拟实现计算器

    javascript模拟实现计算器

    这篇文章主要为大家详细介绍了javascript模拟实现计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • JavaScript实现点击按钮就复制当前网址

    JavaScript实现点击按钮就复制当前网址

    在大量的网站都有这样的功能,当点击一个按钮的时候可以复制当前页面的地址,以此可以方便网站用户对链接的存储,同时也便于网站的推广,下面给大家分享具体实现代码,对js实现点击按钮就复制的相关知识感兴趣的朋友一起学习吧
    2015-12-12
  • 一文了解你不知道的JavaScript生成器篇

    一文了解你不知道的JavaScript生成器篇

    ES6引入了一个新的函数类型,发现它并不符合这种运行到结束的特性。这类新的函数被称为生成器。生成器的出现是我们知道原来有时代码并不会顺利的运行,可以通过暂停的方式进行异步回调,让我们摒弃了此前的认知。本文就来聊聊JavaScript中生成器的相关知识
    2022-11-11
  • JS文件上传时如何使用MD5加密

    JS文件上传时如何使用MD5加密

    这篇文章主要介绍了JS文件上传时如何使用MD5加密,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • javascript 鼠标事件总结

    javascript 鼠标事件总结

    javascript的鼠标事件是个比较庞大的家族。需要的朋友可以参考下。
    2009-12-12
  • js上下视差滚动简单实现代码

    js上下视差滚动简单实现代码

    这篇文章主要为大家详细介绍了js上下视差滚动的简单实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • javascript从定义到执行 你不知道的那些事

    javascript从定义到执行 你不知道的那些事

    这篇文章主要介绍了javascript从定义到执行,你不知道的那些事,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • javaScript中push函数用法实例分析

    javaScript中push函数用法实例分析

    这篇文章主要介绍了javaScript中push函数用法,较为详细的分析了javascript中push函数的功能、定义及使用技巧,需要的朋友可以参考下
    2015-06-06
  • js控制再次点击按钮之间的间隔时间可防止重复提交

    js控制再次点击按钮之间的间隔时间可防止重复提交

    使用js控制再次点击按钮之间的间隔时间可防止重复提交,需要的朋友可以参考下
    2014-08-08

最新评论