图片加载完成前使用Loading.gif代替,加载完成后等比缩放;
下面的代码,造成浏览器不断循环发送请求,最后浏览器不堪负荷,崩溃了。
怎么办呢?
问题出于下面注释行中
function imageScale(ImgD,FitWidth,FitHeight){
var holdImg = "../Images/icons/loading.gif";
var image = new Image(),_width,_hegiht;
image.src = ImgD.src;
ImgD.src = holdImg; /* 问题在这行 和 下面最后一行 */
if(image.width>0 && image.height>0){
if(image.width/image.height>= FitWidth/FitHeight){
if(image.width>FitWidth){
_width=FitWidth;
_height=(image.height*FitWidth)/image.width;
}else{
_width=image.width;
_height=image.height;
}
} else{
if(image.height>FitHeight){
_height=FitHeight;
_width=(image.width*FitHeight)/image.height;
}else{
_width=image.width;
_height=image.height;
}
}
}
ImgD.width = _width;
ImgD.height = _height;
ImgD.src = image.src; /* 问题在这行 和前面的注释行 */
}<img src="http://img.my.csdn.net/uploads/201202/16/266283_1329364024zGY2.jpg" onload="imageScale(this,200,200)" />
下面的代码,造成浏览器不断循环发送请求,最后浏览器不堪负荷,崩溃了。
怎么办呢?
问题出于下面注释行中
function imageScale(ImgD,FitWidth,FitHeight){
var holdImg = "../Images/icons/loading.gif";
var image = new Image(),_width,_hegiht;
image.src = ImgD.src;
ImgD.src = holdImg; /* 问题在这行 和 下面最后一行 */
if(image.width>0 && image.height>0){
if(image.width/image.height>= FitWidth/FitHeight){
if(image.width>FitWidth){
_width=FitWidth;
_height=(image.height*FitWidth)/image.width;
}else{
_width=image.width;
_height=image.height;
}
} else{
if(image.height>FitHeight){
_height=FitHeight;
_width=(image.width*FitHeight)/image.height;
}else{
_width=image.width;
_height=image.height;
}
}
}
ImgD.width = _width;
ImgD.height = _height;
ImgD.src = image.src; /* 问题在这行 和前面的注释行 */
}<img src="http://img.my.csdn.net/uploads/201202/16/266283_1329364024zGY2.jpg" onload="imageScale(this,200,200)" />
解决方案 »
- 命名空间的问题
- window.onbeforeunload 在firefox下无效
- struts2 radio标签 取值
- 请高手指点,以下一段C语言代码, 用 javascript 怎么写? C语言代码转换为javascript, 先跪谢了!!!!!!!
- object 动态添加属性
- 急,Error:'window.button1.style' 为空或不是对象,如何将代码写规范,帮忙改改不会javascript
- 怎么样传值到js中去??
- popup消息框中超级链接的点击问题
- 数据转换问题
- 如何可以将在jsp中的Vector 存放在javascript中的二维数组中?
- 导航栏背景色改变
- 15[^4,\D]这个正则表达式是什么意思?
onload函数里不能再操作src,否则又会执行onload方法,造成了递归使用,是个死循环!!
ImgD.src = image.src; /* 问题在这行 和前面的注释行 */
function imageScale(ImgD,FitWidth,FitHeight){
ImgD.onload=null; //删除绑定事件 避免死循环
//var holdImg = "../Images/icons/loading.gif";根本没必要加载效果,因为你是图片加载成功后执行,根本看不到这个图片
var image = new Image(),_width,_hegiht;
image.src = ImgD.src;
//ImgD.src = holdImg; /* 删除 */
if(image.width>0 && image.height>0){
if(image.width/image.height>= FitWidth/FitHeight){
if(image.width>FitWidth){
_width=FitWidth;
_height=(image.height*FitWidth)/image.width;
}else{
_width=image.width;
_height=image.height;
}
} else{
if(image.height>FitHeight){
_height=FitHeight;
_width=(image.width*FitHeight)/image.height;
}else{
_width=image.width;
_height=image.height;
}
}
}
ImgD.width = _width;
ImgD.height = _height;
ImgD.src = image.src;
}
</script>
加一个afterrender event的监听,图片显示完毕后,则去掉loading.gif。
var pic=document.getElementsByTagName("img");// 最好指定范围的图片,以免全部改了。
var temp,arr=[];//数组
for(var i=0;i<pic.length;i++){
arr[i] = new Image();
arr[i].onload=(function(n){
return function(){
//.........其他代码
pic[n].src=arr[n].src;//图片加载完后,还原图片。必须使用闭包函数!
}
})(i);
temp=pic[i].src;
pic[i].src="http://avatar.profile.csdn.net/6/6/6/1_ftiger.jpg";//加载效果,不管图片有没有加载成功,先替换再说
arr[i].src = temp;//得到原图片地址
}
</script>