图片预载ImageLoader 1.1 Release

作者:dknt 时间:2008-11-04 20:04:00 

特点

1.图片预载入,载入后再显示。意图一次呈现,不会让一块一块下载破坏你的页面,绝佳的用户体验,颠覆传统的浏览器呈现图片的处理方式(需要后续函数支持)。

2.不会因载入图片造成脚本暂停假死,完全另一线程进行。不影响主程序流程。

3.提供及时的反馈,包括两方面的内容:1.正在载入什么图片 2.当前的百分数进度。大大提高留住用户眼球的概率,不会让用户因为苦等而离开。

4.容错支持,即使某个图片没有成功下载,也可以设置超时时间以便处理下一个图片。

5.多变的参数类型,尽最大可能方便使用。

相关文章:好用的JS图片预加载类


// 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;
    }
}

标签:图片预载,预载,图片,js
0
投稿

猜你喜欢

  • Python模块结构与布局操作方法实例分析

    2021-11-26 10:49:31
  • bootstrap实现点击删除按钮弹出确认框的实例代码

    2023-08-28 09:36:20
  • python 还原梯度下降算法实现一维线性回归

    2023-10-09 21:53:42
  • 一文带你看懂Vue Hook和React Hook

    2024-04-22 13:24:02
  • SQL参数化查询的另一个理由 命中执行计划

    2012-08-21 10:31:16
  • python ImageDraw类实现几何图形的绘制与文字的绘制

    2023-10-14 10:58:13
  • 用JS找出字符串中出现次数最多的字母

    2007-11-12 13:40:00
  • 详解如何利用Python制作24点小游戏

    2022-02-04 17:22:42
  • JS中setTimeout()的用法详解

    2023-06-26 20:55:17
  • 使用用SQL语句从电脑导入图片到数据库的方法

    2024-01-13 22:29:00
  • 关于Ajax在浏览器中产生前进后退的实现方法

    2010-01-29 13:01:00
  • 如何把图片上传到数据库中并显示出来?

    2009-11-06 13:50:00
  • Python Pandas读取Excel日期数据的异常处理方法

    2021-12-10 11:22:55
  • IE6终极备忘单——对IE6单独兼容[译]

    2010-01-21 18:34:00
  • 浅析Python是如何实现集合的

    2022-05-16 03:38:58
  • 在EF中使用MySQL的方法及常见问题

    2024-01-28 11:22:03
  • SQL语句练习实例之七 剔除不需要的记录行

    2011-11-03 16:50:51
  • 在python中利用pycharm自定义代码块教程(三步搞定)

    2022-11-20 01:00:50
  • Go 微服务开发框架DMicro设计思路详解

    2024-05-22 17:48:31
  • Python操作Access数据库基本步骤分析

    2024-01-18 22:32:20
  • asp之家 网络编程 m.aspxhome.com