var ImagePreview = function(file, img, options) {
this.file = $(file);//文件对象
this.img = $(img);//预览图片对象
this._preload = null;//预载图片对象
this._data = "";//图像数据
this._upload = null;//remote模式使用的上传文件对象
var opt = this._setOptions(options);
this.action = opt.action;
this.timeout = opt.timeout;
this.ratio = opt.ratio;
this.maxWidth = opt.maxWidth;
this.maxHeight = opt.maxHeight;
this.onCheck = opt.onCheck;
this.onShow = opt.onShow;
this.onErr = opt.onErr;
//设置数据获取程序
this._getData = this._getDataFun(opt.mode);
//设置预览显示程序
this._show = opt.mode !== "filter" ? this._simpleShow : this._filterShow;
};
//根据浏览器获取模式
ImagePreview.MODE = $B.ie7 || $B.ie8 ? "filter" :
$B.firefox ? "domfile" :
$B.opera || $B.chrome || $B.safari ? "remote" : "simple";
//透明图片
ImagePreview.TRANSPARENT = $B.ie7 || $B.ie6 ?
"mhtml:" + document.scripts[document.scripts.length - 1].getAttribute("src", 4) + "!blankImage" :
"data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
ImagePreview.prototype = {
//設定預設屬性
_setOptions: function(options) {
this.options = {//預設值 ratio: 0,//自訂比例
maxWidth: 0,//縮圖寬度
maxHeight: 0,//縮圖高度
預覽偵測時執行
onShow: function(){},//預覽圖片時執行
onErr: function(){},//預覽錯誤時執行
//以下在使用🎜> action: undefined,//設定action
timeout: 0//設定逾時(0設定為不設定)
};
},
//開始預覽
preview: function() {
if ( this.file && false !== this.onCheck() ) {
_ . _getData() );
}
},
//根據mode回傳資料取得程式
_getDataFun: function(mode) {
switch (M88) {
return this._filterData;
case "domfile" :
return this._domfileData;
tecase" simple " :
default :
return this._simpleData;
}
},
//濾鏡資料取得程式
_filterData. select();
try{
return document.selection.createRange().text;
} finally { document.selection.empty(); }
}, _domfileData: function() {
return this.file.files[0].getAsDataURL();
},
//遠端資料取得程式
_remoteData: function(teData: function( {
this._setUpload();
this._upload && this._upload.upload();
},
//一般資料取得程式
_simpleData: f🎜> //一般資料取得程式
_simpleData: function( > return this.file.value;
},
//設定remote模式的上傳檔案物件
_setUpload: function() {
if ( !this._upload &&this.action !== undefined && typeof QuickUpload === "function" ) {
var oThis = this;
this._upload = new QuickUpload(this.file, {
this.action = oThis.action; this.timeout = oThis.timeout;
var parameter = this.parameter;
parameter.ratio = oThis.ratio;
¢ > parameter.height = oThis.maxHeight;
},
onFinish: function(iframe){
•@. );
}catch(e){ oThis._error("remote error"); }
},
onTimeout: function(){ oThis._error("timeout error"); }
}
},
//預覽程式
_preview: function(data) {
//空值或相同的值不執行顯示
! data !== this._data ) {
this._data = data; this._show();
}
},
//設定一般預載圖片物件
_simplePreload: function() {
if ( !this._preload ) {
var preload = this._preload = new Image(), oThis = this,
._data, this.width, this.height ); };
this._onload = function(){ this.onload = null; onload.call(this); }
preload.onload = $B.ie ? this._onload : onload;
preload.onerror = function(){ oThis._error(); };
} else if ( $B.ie ) {
_onload;
}
},
//一般顯示
_simpleShow: function() {
this._simplePreload();
🎜> this._simplePreload();
.thispreload.src; 🎜> },
//設定濾鏡預載圖片物件
_filterPreload: function() {
if ( !this._preload ) {
var preload = .docum .createElement("div");
//隱藏並設定濾鏡
$D.setStyle( preload, {
width: "1px", height: "1px", filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='image')" //插入body
var body = document.body; body.insertBefore( preload, body.childNodes[0] );
}
},
//過濾顯示
_filterShow: function() {
this._filterPreload();
var preload = this._preload,
data = this._.replace( '"%]/g, function(s){ return escape(escape(s)); });
嘗試{
preload.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = data ;
}catch(e){ this._error("filter error"); return; }
//設定過濾器並顯示
this.img.style.filter = "progid:DXImageTransform. Microsoft .AlphaImageLoader(sizingMethod='scale',src="" data "")";
this._imgShow( ImagePreview.TRANSPARENT, preload.offsetWidth, preload.offsetHeight );
},
// 顯示預覽
_imgShow: function(src, width, height) {
var img = this.img, style = img.style,
ratio = Math.max( 0, this.this ) | Math Math.min( 1,
Math.max( 0, this.maxWidth ) / 寬度|| 1,
Math.max( 0, this.maxHeight ) / 高度 1 //設定頁尺寸
style.width = Math.round( width *ratio ) "px";
style.height = Math.round(height *ratio ) "px";
/ /設定src
img.src = src;
this.onShow();
},
// 調查程序
dispose: function(> // 調查程序
dispose: function() {上傳檔案物件
if ( this._upload ) {
this._upload.dispose(); this._upload = null;
}
//調查預載圖片🎜>this var preload = this._preload,parent = preload.parentNode;
this ._preload = preload.onload = preload.onerror = null;
> }
//調查相關對象
this.file = this.img = null;
},
//錯誤
_error: function(err) {
錯誤
。程式碼如下:
<script><BR>var ip = new ImagePreview( $$("idFile"), $$("idImg"), {<BR> maxWidth: 200, maxHeight: 2000 , action: "ImagePreview.ashx"<BR>});</script>