YPreLoad
Javascript库
发布我的控件系列:图片预加载控件YPreLoad v1.0
介绍
大家好!很高兴向大家介绍我的图片预加载控件YPreLoad。它可以帮助您预加载图片,并且能显示加载的进度,在预加载完成后调用指定的方法。
YPreLoad控件由一个名为PreLoadImg的类组成。该类的构造函数为:PreLoadImg(images, onstep, onload)
依赖库
用法

- new PreLoadImg(
- /**
- * 图片数据
- * id为图片id号,url为图片地址
- */
- [
- { id: "a1", url: "a1.png" },
- { id: "a2", url: "a2.png" }
- ],
- /**
- * 获得加载进度
- * @param currentLoad 已加载的图片数量
- * @param imgCount 图片总数
- */
- function (currentLoad, imgCount) {
- },
- /**
- * 加载完成后调用
- */
- function () {
- }
- );

效果演示
代码

- var PreLoadImg = YYC.Class({
- Init: function (images, onstep, onload) {
- this._checkImages(images);
- this.config = {
- images: images || [],
- onstep: onstep || function () {},
- onload: onload || function () {}
- };
- this._imgs = {};
- this.imgCount = this.config.images.length;
- this.currentLoad = 0;
- this.timerID = 0;
- this.loadImg();
- },
- Private: {
- _imgs: {},
- _checkImages: function (images) {
- var i = null;
- for (var i in images) {
- if (images.hasOwnProperty(i)) {
- if (images[i].id === undefined || images[i].url === undefined) {
- throw new Error("应该包含id和url属性");
- }
- }
- }
- },
- _bind: function (object, fun) {
- return function () {
- return fun.apply(object, arguments);
- };
- }
- },
- Public: {
- imgCount: 0,
- currentLoad: 0,
- timerID: 0,
- /**
- * 通过图片id号来获得图片对象
- * @param id 图片id号
- * @returns {*} 图片对象
- */
- get: function (id) {
- return this._imgs[id];
- },
- loadImg: function () {
- var c = this.config,
- img = null,
- i,
- self = this,
- image = null;
- for (i = 0; i < c.images.length; i++) {
- img = c.images[i];
- image = this._imgs[img.id] = new Image();
- image.onload = function () {
- this.onload = null;
- self._bind(self, self.onload)();
- };
- image.src = img.url;
- this.timerID = (function (i) {
- return setTimeout(function () {
- if (i == self.currentLoad) {
- image.src = img.url;
- }
- }, 500);
- })(i);
- }
- },
- onload: function (i) {
- clearTimeout(this.timerID);
- this.currentLoad++;
- this.config.onstep(this.currentLoad, this.imgCount);
- if (this.currentLoad === this.imgCount) {
- this.config.onload(this.currentLoad);
- }
- },
- dispose: function () {
- var i,
- _imgs = this._imgs;
- for (i in _imgs) {
- _imgs[i].onload = null;
- _imgs[i] = null;
- }
- this.config = null;
- }
- }
- });

下载
YPreLoad的更多相关文章
随机推荐
- 第22章 职责链模式(Chain of Responsibility)
原文 第22章 职责链模式(Chain of Responsibility) 职责链模式 导读:职责链模式是一个既简单又复杂的设计模式,刚开始学习这个设计模式的时候光示例都看了好几遍.就为了理清里面的 ...
- crawler_java应用集锦9:httpclient4.2.2的几个常用方法,登录之后访问页面问题,下载文件_设置代理
在工作中要用到android,然后进行网络请求的时候,打算使用httpClient. 总结一下httpClient的一些基本使用. 版本是4.2.2. 使用这个版本的过程中,百度很多,结果都是出现的o ...
- Swift学习笔记(一)搭配环境以及代码运行成功
原文:Swift学习笔记(一)搭配环境以及代码运行成功 1.Swift是啥? 百度去!度娘告诉你它是苹果最新推出的编程语言,比c,c++,objc要高效简单.能够开发ios,mac相关的app哦!是苹 ...
- C# Parse和Convert的区别分析(转)
大家都知道在进行类型转换的时候有连个方法供我们使用就是Convert.to和*.Parse,但是疑问就是什么时候用C 什么时候用P 通俗的解释大家都知道: Convert 用来转换继承自object类 ...
- Cracking Microservices practices
微服务最佳实践 英文原文:Cracking Microservices practices 在我还不知道什么叫微服务架构的时候我就使用过它.以前,我写了一些管道程序(pipeline applicat ...
- linux简单的数据包捕获分析
有时我们会遇到一些问题,需要捕捉数据包分析,当手头有没有专业的抓图工具,您可以使用tcpdump相反,看看(一般版本附带这个工具) 比如,我们要分析eth0与接口192.168.7.188 这个对象I ...
- 采用malloc分别分配2KB个人空间,然后,realloc调整到6KB、1MB、3MB、10MB场地,分别这五内存“A”、“B”、“C”、“D”、“E”灌装
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<malloc.h> i ...
- iptables的配置文件/etc/sysconfig/iptables不存在怎么办
iptables的配置文件/etc/sysconfig/iptables不存在怎么办 首先要看一下iptables是否安装了,使用service iptables status或yum info ip ...
- 初创互联网公司简明创业指南 - YC新掌门Sam Altman
本文只是一个创业指南的简明版 - 更详细的版本请查看:http://startupclass.samaltman.com 创业之前,你更应该去拥有一个好的创意,而不是一个公司.如果开始前你拥有一个好的 ...
- dom03
鼠标事件: 键盘事件: //通过class获取元素,封装一个通过class获取元素的方法 //IE10以下不支持document.getElementByClass() function getByC ...