Javascript兑现图片预加载【回调函数,多张图片】 (转载)
2014-01-23 10:25
330 查看
Javascript实现图片预加载【回调函数,多张图片】
使用JS实现一组图片动画效果或者使用HTML5 Canvas渲染一系列图片等案例中,需要图片全部加载完成方可运行动画效果。此时程序中就会涉及多张图片预加载代码。当接二连三的案例中都涉及图片预加载时,就需要考虑把这个功能封装为一个通用方法。
(1)下面为JS实现图片预加载方法loadImages():
(2)在JS中调用预加载函数:
注意事项:
(1)先绑定image.onload事件,后加载图片
原因:如果图片从缓存中加载,速度非常快以至于没来得及绑定事件就加载完毕,自然不会触发绑定事件。
(2)for...in循环 与 for循环的区别
for循环用于迭代数组(array)
for...in循环用于迭代对象(object, {})或者关联数组(hash array)
原文地址:http://www.myexception.cn/javascript/408426.html
下面是小练习:
使用JS实现一组图片动画效果或者使用HTML5 Canvas渲染一系列图片等案例中,需要图片全部加载完成方可运行动画效果。此时程序中就会涉及多张图片预加载代码。当接二连三的案例中都涉及图片预加载时,就需要考虑把这个功能封装为一个通用方法。
(1)下面为JS实现图片预加载方法loadImages():
//实现一系列图片的预加载 //参数sources:图片信息关联数组 //参数callback:回调函数——图片预加载完成后立即执行此函数。 function loadImages(sources, callback){ var count = 0, images ={}, imgNum = 0; for(src in sources){ imgNum++; } for(src in sources){ images[src] = new Image(); images[src].onload = function(){ if(++count >= imgNum){ callback(images); } } images[src].src = sources[src]; } }
(2)在JS中调用预加载函数:
//存储图片链接信息的关联数组 var sources = { ietoHell : "img/IEtoHell.jpg", fuckIE : "img/fuckIE.jpg" }; //调用图片预加载函数,实现回调函数 loadImages(sources, function(images){ //TO-DO something ctx.drawImage(images.ietoHell, 20,20,100,100); ctx.drawImage(images.fuckIE, 140,20,100,100); });
注意事项:
(1)先绑定image.onload事件,后加载图片
images[src] = new Image(); images[src].onload = function(){ if(++count >= imgNum){ callback(images); } } images[src].src = sources[src];
原因:如果图片从缓存中加载,速度非常快以至于没来得及绑定事件就加载完毕,自然不会触发绑定事件。
(2)for...in循环 与 for循环的区别
for循环用于迭代数组(array)
for...in循环用于迭代对象(object, {})或者关联数组(hash array)
原文地址:http://www.myexception.cn/javascript/408426.html
下面是小练习:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> </head> <body> <a onClick="newload()">点击</a> <img src="myFocus_white.gif" id="im0" /> <img src="myFocus_white.gif" id="im1"/> <img src="myFocus_white.gif" id="im2"/> <img src="myFocus_white.gif" id="im3"/> <img src="myFocus_white.gif" id="im4"/> <img src="myFocus_white.gif" id="im5"/> <img src="myFocus_white.gif" id="im6"/> <img src="myFocus_white.gif" id="im7"/> </body> <script type="text/javascript"> //存储图片链接信息的关联数组 var sources = { im0 : "http://www.leyoo.com/Public/nImg/Zhuanti/20131231/run_img_1.png", im1 : "http://www.leyoo.com/Public/nImg/Zhuanti/20131231/run_img_2.png", im2 : "http://www.leyoo.com/Public/nImg/Zhuanti/20131231/run_img_3.png", im3 : "http://www.leyoo.com/Public/nImg/Zhuanti/20131231/run_img_4.png", im4 : "http://www.leyoo.com/Public/nImg/Zhuanti/20131231/run_img_5.png", im5 : "http://www.leyoo.com/Public/nImg/Zhuanti/20131231/run_img_6.png", im6 : "http://www.leyoo.com/Public/nImg/Zhuanti/20131231/run_img_7.png", im7 : "http://www.leyoo.com/Public/nImg/Zhuanti/20131231/run_img_8.png" }; //用于计算对象成员和数组成员个数 function count(o){ var t = typeof o; if(t == 'string'){ return o.length; }else if(t == 'object'){ var n = 0; for(var i in o){ n++; } return n; } return false; } //调用图片预加载函数,实现回调函数 loadImages(sources, function(images){ //TO-DO something for(var i=0; i<count(images); i++) { oid = "im"+i; document.getElementById(oid).src = images[oid].src; }}); function loadImages(sources, callback){ var count = 0, images ={}, imgNum = 0; for(src in sources){ imgNum++; } for(var src in sources){ images[src] = new Image(); //document.getElementById(src).src = images[src].src; images[src].onload = function(){ if(++count >= imgNum){ images[src].onload= null; callback(images); } } images[src].src = sources[src]; } } function newload(){alert("预加载到底完成没?")} </script> </html>
相关文章推荐
- Javascript实现图片预加载【回调函数,多张图片】
- Javascript实现图片预加载【回调函数,多张图片】
- javascript图片延迟加载(转载)
- JavaScript控制图片加载完成后调用回调函数的方法
- Javascript实现图片预加载【回调函数,多张图片】
- CSS和JavaScript以及Ajax实现预加载图片的方法及优缺点分析
- JS 实现图片的预加载(转载)
- 利用JavaScript及jQuery实现图片预加载的简单方法
- javascript图片预加载技术
- Javascript图片预加载详解
- 利用简洁的图片预加载组件提升h5移动页面的用户体验--转载
- 利用CSS、JavaScript及Ajax实现图片预加载的三大方法及优缺点分析
- Javascript实现图片的预加载的完整实现
- javascript瀑布流式图片懒加载实例解析与优化
- javascript使图片加载进度实时显示
- 使用JavaScript判断图片是否加载完成的三种实现方式
- javascript实现瀑布流动态加载图片原理
- JavaScript 学习笔记之一jQuery写法图片等比缩放以及预加载
- 【原】HTML页面元素加载顺序研究报告(4)----用JavaScript插入图片
- 再谈javascript图片预加载技术