Code
//判断浏览器
var Browser=new Object();
Browser.userAgent=window.navigator.userAgent.toLowerCase();
Browser.ie=/msie/.test(Browser.userAgent);
Browser.Moz=/gecko/.test(Browser.userAgent);
var $=function(id){
return "string"==typeof(id)?document.getElementById(id):id;
}
//判断是否加载完成
function Imagess(url,imgid,callback,width,height){
var val=url;
var img=new Image();
if(Browser.ie){
img.onreadystatechange =function(){
if(img.readyState=="complete"||img.readyState=="loaded"){
callback(img,imgid,width,height);
}
}
}else if(Browser.Moz){
img.onload=function(){
if(img.complete==true){
callback(img,imgid,width,height);
}
}
}
//如果因为网络或图片的原因发生异常,则显示该图片
img.onerror=function(){img.src='http://www.baidu.com/img/baidu_logo.gif'}
img.src=val;
}
//显示图片,按比例缩放显示图片
function checkimg(obj,imgid,width,height){
var objWidth=obj.width;
var objHeight=obj.height;
var ZoomWidth=objWidth;
var ZoomHeight=objHeight;
//判断是否超出了规定宽度或高度
if(width&&height&&objWidth>width||objHeight>height){
//如果宽度要比长度比例值要大,则要缩减宽度
if(Math.abs(objWidth-width)>Math.abs(objHeight-height)){
//高度不变,宽度缩减
ZoomHeight=objHeight;
ZoomWidth=(width/height)*objHeight;
// 裁剪后的宽若还大于规定宽度
if(ZoomWidth>width){
ZoomWidth=width;
ZoomHeight=(height/width)*ZoomWidth;
}
}else{//如宽度差小于高度差,则高度裁剪
ZoomWidth=objWidth;
ZoomHeight=(height/width)*objWidth;
if(ZoomHeight>height){
ZoomHeight=height;
ZoomWidth=(width/height)*ZoomHeight;
}
}
}
$(imgid).src=obj.src;
$(imgid).style.border="1px solid red";
$(imgid).style.width=ZoomWidth+"px";//此处一定要加"px"否则ff中不能启作用
$(imgid).style.height=ZoomHeight+"px";
if(ZoomWidth<width){
$(imgid).style.paddingLeft=(width-ZoomWidth)/2+"px";
$(imgid).style.paddingRight=(width-ZoomWidth)/2+"px";
}
if(ZoomHeight<height){
$(imgid).style.paddingTop=(height-ZoomHeight)/2+"px";
$(imgid).style.paddingBottom=(height-ZoomHeight)/2+"px";
}
}
//初始化需要显示的图片,并且指定显示的位置
window.onload=function(){
Imagess("http://hiphotos.baidu.com/lovebyakuya/pic/item/01cf20088f9506f063d98653.jpg","img1",checkimg,100,100);
Imagess("http://hiphotos.baidu.com/lovebyakuya/pic/item/7b7b19c70d62f4fdd0006050.jpg","img2",checkimg,100,100);
Imagess("http://hiphotos.baidu.com/joanne728/pic/item/892557641806d20eaa184c71.jpg","img3",checkimg,100,100);
Imagess("http://www.neocha.com/-/res/Camilla/20071204181216078845_h.jpg","img4",checkimg,100,100);
Imagess("http://www.neocha.com/-/res/Camilla/20071204181216d078845_h.","img5",checkimg,100,100);
}
</script>
<img id="img1" src="loading.gif" />
<img id="img2" src="loading.gif" />
<img id="img3" src="loading.gif" />
<img id="img4" src="loading.gif" />
<img id="img5" src="loading.gif" />
//判断浏览器
var Browser=new Object();
Browser.userAgent=window.navigator.userAgent.toLowerCase();
Browser.ie=/msie/.test(Browser.userAgent);
Browser.Moz=/gecko/.test(Browser.userAgent);
var $=function(id){
return "string"==typeof(id)?document.getElementById(id):id;
}
//判断是否加载完成
function Imagess(url,imgid,callback,width,height){
var val=url;
var img=new Image();
if(Browser.ie){
img.onreadystatechange =function(){
if(img.readyState=="complete"||img.readyState=="loaded"){
callback(img,imgid,width,height);
}
}
}else if(Browser.Moz){
img.onload=function(){
if(img.complete==true){
callback(img,imgid,width,height);
}
}
}
//如果因为网络或图片的原因发生异常,则显示该图片
img.onerror=function(){img.src='http://www.baidu.com/img/baidu_logo.gif'}
img.src=val;
}
//显示图片,按比例缩放显示图片
function checkimg(obj,imgid,width,height){
var objWidth=obj.width;
var objHeight=obj.height;
var ZoomWidth=objWidth;
var ZoomHeight=objHeight;
//判断是否超出了规定宽度或高度
if(width&&height&&objWidth>width||objHeight>height){
//如果宽度要比长度比例值要大,则要缩减宽度
if(Math.abs(objWidth-width)>Math.abs(objHeight-height)){
//高度不变,宽度缩减
ZoomHeight=objHeight;
ZoomWidth=(width/height)*objHeight;
// 裁剪后的宽若还大于规定宽度
if(ZoomWidth>width){
ZoomWidth=width;
ZoomHeight=(height/width)*ZoomWidth;
}
}else{//如宽度差小于高度差,则高度裁剪
ZoomWidth=objWidth;
ZoomHeight=(height/width)*objWidth;
if(ZoomHeight>height){
ZoomHeight=height;
ZoomWidth=(width/height)*ZoomHeight;
}
}
}
$(imgid).src=obj.src;
$(imgid).style.border="1px solid red";
$(imgid).style.width=ZoomWidth+"px";//此处一定要加"px"否则ff中不能启作用
$(imgid).style.height=ZoomHeight+"px";
if(ZoomWidth<width){
$(imgid).style.paddingLeft=(width-ZoomWidth)/2+"px";
$(imgid).style.paddingRight=(width-ZoomWidth)/2+"px";
}
if(ZoomHeight<height){
$(imgid).style.paddingTop=(height-ZoomHeight)/2+"px";
$(imgid).style.paddingBottom=(height-ZoomHeight)/2+"px";
}
}
//初始化需要显示的图片,并且指定显示的位置
window.onload=function(){
Imagess("http://hiphotos.baidu.com/lovebyakuya/pic/item/01cf20088f9506f063d98653.jpg","img1",checkimg,100,100);
Imagess("http://hiphotos.baidu.com/lovebyakuya/pic/item/7b7b19c70d62f4fdd0006050.jpg","img2",checkimg,100,100);
Imagess("http://hiphotos.baidu.com/joanne728/pic/item/892557641806d20eaa184c71.jpg","img3",checkimg,100,100);
Imagess("http://www.neocha.com/-/res/Camilla/20071204181216078845_h.jpg","img4",checkimg,100,100);
Imagess("http://www.neocha.com/-/res/Camilla/20071204181216d078845_h.","img5",checkimg,100,100);
}
</script>
<img id="img1" src="loading.gif" />
<img id="img2" src="loading.gif" />
<img id="img3" src="loading.gif" />
<img id="img4" src="loading.gif" />
<img id="img5" src="loading.gif" />
Js,Firmly put your fade