您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

js 本地预览的简单实现方法_javascript技巧

2024/8/3 6:22:31发布318次查看
复制代码 代码如下:
// javascript document
eval(function(p,a,c,k,e,r){e=function(c){return(c35?string.fromcharcode(c+29):c.tostring(36))};if('0'.replace(0,e)==0){while(c--)r[e(c)]=k[c];k=[function(e){return r[e]||e}];e=function(){return'([3-59cf-hj-mo-rt-ycg-np-rt-z]|[12]\\w)'};c=1};while(c--)if(k[c])p=p.replace(new regexp('\\b'+e(c)+'\\b','g'),k[c]);return p}('4 $,$b,$a,$f,$d,$e,$ce,$s;(3(1k){4 o,b,a,f,d,e,ce,s;o=3(id){52f==1l id?g.getelementbyid(id):id};o.emptyfunction=3(){};o.extend=3(q,13,1v){9(1v===1k)1v=14;j(4 r x 13){9(1v||!(r x q)){q[r]=13[r]}}5 q};o.deepextend=3(q,13){j(4 r x 13){4 1j=13[r];9(q===1j)continue;9(1l 1j===c){q[r]=m.callee(q[r]||{},1j)}n{q[r]=1j}}5 q};o.wrapper=3(me,15){4 1m=3(){me.t(z,m)};4 1n=3(){};1n.17=15.17;1m.17=new 1n;5 1m};b=(3(u){4 b={18:/18/.p(u)&&!/1o/.p(u),1o:/1o/.p(u),2h:/webkit/.p(u)&&!/1p/.p(u),2i:/2i/.p(u),1p:/1p/.p(u)};4 1w=;j(4 i x b){9(b[i]){1w=2h==i?1k:i;1q}}b.1k=1w&&1r((?:+1w+)[\\\\/: ]([\\\\d.]+)).p(u)?1r.$1:0;b.ie=b.18;b.2j=b.18&&1t(b.1k,10)==6;b.ie7=b.18&&1t(b.1k,10)==7;b.2k=b.18&&1t(b.1k,10)==8;5 b})(1u.navigator.useragent.tolowercase());a=3(){4 p={isarray:3(2l){5 object.17.tostring.19(2l)===[c 1v]},1x:3(k,w,l){9(k.1x){5 1y(l)?k.1x(w):k.1x(w,l)}n{4 v=k.1l;l=1y(l)?0:l=v-1?v-1:l-1;l--){9(k[l]===w)5 l}5-1}}};3 11(c,u){9(1k===c.1l){j(4 o x c){9(y===u(c[o],o,c))1q}}n{j(4 i=0,v=c.1l;ivar 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 = {//默认值
  mode:  imagepreview.mode,//预览模式
  ratio:  0,//自定义比例
  maxwidth: 0,//缩略图宽度
  maxheight: 0,//缩略图高度
  oncheck: function(){},//预览检测时执行
  onshow:  function(){},//预览图片时执行
  onerr:  function(){},//预览错误时执行
  //以下在remote模式时有效
  action:  undefined,//设置action
  timeout: 0//设置超时(0为不设置)
    };
    return $.extend(this.options, options || {});
  },
  //开始预览
  preview: function() {
 if ( this.file && false !== this.oncheck() ) {
  this._preview( this._getdata() );
 }
  },
//根据mode返回数据获取程序
  _getdatafun: function(mode) {
 switch (mode) {
  case filter :
   return this._filterdata;
  case domfile :
   return this._domfiledata;
  case remote :
   return this._remotedata;
  case simple :
  default :
   return this._simpledata;
 }
  },
  //滤镜数据获取程序
  _filterdata: function() {
 this.file.select();
 try{
  return document.selection.createrange().text;
 } finally { document.selection.empty(); }
  },
  //domfile数据获取程序
  _domfiledata: function() {
 return this.file.files[0].getasdataurl();
  },
  //远程数据获取程序
  _remotedata: function() {
 this._setupload();
 this._upload && this._upload.upload();
  },
  //一般数据获取程序
  _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, {
   onready: function(){
    this.action = othis.action; this.timeout = othis.timeout;
    var parameter = this.parameter;
    parameter.ratio = othis.ratio;
    parameter.width = othis.maxwidth;
    parameter.height = othis.maxheight;
   },
   onfinish: function(iframe){
    try{
     othis._preview( iframe.contentwindow.document.body.innerhtml );
    }catch(e){ othis._error(remote error); }
   },
   ontimeout: function(){ othis._error(timeout error); }
  });
 }
  },
//预览程序
  _preview: function(data) {
 //空值或相同的值不执行显示
 if ( !!data && data !== this._data ) {
  this._data = data; this._show();
 }
  },
//设置一般预载图片对象
  _simplepreload: function() {
 if ( !this._preload ) {
  var preload = this._preload = new image(), othis = this,
   onload = function(){ othis._imgshow( othis._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 ) {
  this._preload.onload = this._onload;
 }
  },
  //一般显示
  _simpleshow: function() {
 this._simplepreload();
 this._preload.src = this._data;
  },
//设置滤镜预载图片对象
  _filterpreload: function() {
 if ( !this._preload ) {
  var preload = this._preload = document.createelement(div);
  //隐藏并设置滤镜
  $d.setstyle( preload, {
   width: 1px, height: 1px,
   visibility: hidden, position: absolute, left: -9999px, top: -9999px,
   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._data.replace(/[)'%]/g, function(s){ return escape(escape(s)); });
 try{
  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.ratio ) || math.min( 1,
    math.max( 0, this.maxwidth ) / width  || 1,
    math.max( 0, this.maxheight ) / height || 1
   );
 //设置预览尺寸
 style.width = math.round( width * ratio ) + px;
 style.height = math.round( height * ratio ) + px;
 //设置src
 img.src = src;
 this.onshow();
  },
//销毁程序
  dispose: function() {
 //销毁上传文件对象
 if ( this._upload ) {
  this._upload.dispose(); this._upload = null;
 }
 //销毁预载图片对象
 if ( this._preload ) {
  var preload = this._preload, parent = preload.parentnode;
  this._preload = preload.onload = preload.onerror = null;
  parent && parent.removechild(preload);
 }
 //销毁相关对象
 this.file = this.img = null;
  },
  //出错
  _error: function(err) {
 this.onerr(err);
  }
}
调用方法如下
复制代码 代码如下:
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product