DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> jquery 圖片預加載 自動等比例縮放插件
jquery 圖片預加載 自動等比例縮放插件
編輯:JQuery特效代碼     

 

文件打包下載

代碼如下:
/*
**************圖片預加載插件******************
///作者:沒劍(2008-06-23)
///http://regedit.cnblogs.com

///說明:在圖片加載前顯示一個加載標志,當圖片下載完畢後顯示圖片出來
可對圖片進行是否自動縮放功能
此插件使用時可讓頁面先加載,而圖片後加載的方式,
解決了平時使用時要在圖片顯示出來後才能進行縮放時撐大布局的問題
///參數設置:
scaling 是否等比例自動縮放
width 圖片最大高
height 圖片最大寬
loadpic 加載中的圖片路徑
*/
jQuery.fn.LoadImage=function(scaling,width,height,loadpic){
if(loadpic==null)loadpic="load3.gif";
return this.each(function(){
var t=$(this);
var src=$(this).attr("src")
var img=new Image();
//alert("Loading")
img.src=src;
//自動縮放圖片
var autoScaling=function(){
if(scaling){

if(img.width>0 && img.height>0){
if(img.width/img.height>=width/height){
if(img.width>width){
t.width(width);
t.height((img.height*width)/img.width);
}else{
t.width(img.width);
t.height(img.height);
}
}
else{
if(img.height>height){
t.height(height);
t.width((img.width*height)/img.height);
}else{
t.width(img.width);
t.height(img.height);
}
}
}
}
}
//處理ff下會自動讀取緩存圖片
if(img.complete){
//alert("getToCache!");
autoScaling();
return;
}
$(this).attr("src","");
var loading=$("<img alt=\"加載中\" title=\"圖片加載中\" src=\""+loadpic+"\" />");

t.hide();
t.after(loading);
$(img).load(function(){
autoScaling();
loading.remove();
t.attr("src",this.src);
t.show();
//alert("finally!")
});

});
}

使用說明:
$("div img").LoadImage(true,120,90);



XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved