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


Posted in Javascript onFebruary 18, 2014
// JavaScript Document
eval(function(p,a,c,k,e,r){e=function(c){return(c<62?'':e(parseInt(c/62)))+((c=c%62)>35?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){5"2f"==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<0?1z.2m(l)+V:1z.2n(l);J(;l<V;l++){9(K[l]===W)5 l}5-1}},1A:3(K,W,l){9(K.1A){5 1y(l)?K.1A(W):K.1A(W,l)}N{4 V=K.1l;l=1y(l)||l>=V-1?V-1:l<0?1z.2m(l)+V:1z.2n(l);J(;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;i<V;i++){9(i x c){9(y===u(c[i],i,c))1Q}}}};11({2o:3(c,u,t){11(c,3(){u.T(t,M)})},map:3(c,u,t){4 p=[];11(c,3(){p.2p(u.T(t,M))});5 p},1B:3(c,u,t){4 p=[];11(c,3(2q){u.T(t,M)&&p.2p(2q)});5 p},every:3(c,u,t){4 p=14;11(c,3(){9(!u.T(t,M)){p=y;5 y}});5 p},some:3(c,u,t){4 p=y;11(c,3(){9(u.T(t,M)){p=14;5 y}});5 p}},3(2r,o){p[o]=3(c,u,t){9(c[o]){5 c[o](u,t)}N{5 2r(c,u,t)}}});5 p}();F=(3(){4 1a=1V.17.1a;5{bind:3(1C,t){4 1b=1a.19(M,2);5 3(){5 1C.T(t,1b.2s(1a.19(M)))}},bindAsEventListener:3(1C,t){4 1b=1a.19(M,2);5 3(j){5 1C.T(t,[E.1m(j)].2s(1b))}}}})();D={1D:3(r){4 1c=r?r.2t:G;5 1c.2u.2v||1c.1d.2v},1E:3(r){4 1c=r?r.2t:G;5 1c.2u.2w||1c.1d.2w},1W:G.1n?3(a,b){5!!(a.2x(b)&16)}:3(a,b){5 a!=b&&a.1W(b)},H:3(r){4 q=0,L=0,X=0,Y=0;9(!r.2y||B.2k){4 n=r;while(n){q+=n.offsetLeft,L+=n.offsetTop;n=n.offsetParent};X=q+r.1X;Y=L+r.1Y}N{4 H=r.2y();q=X=D.1E(r);L=Y=D.1D(r);q+=H.q;X+=H.X;L+=H.L;Y+=H.Y};5{"q":q,"L":L,"X":X,"Y":Y}},clientRect:3(r){4 H=D.H(r),1Z=D.1E(r),20=D.1D(r);H.q-=1Z;H.X-=1Z;H.L-=20;H.Y-=20;5 H},1e:G.1n?3(g){5 G.1n.2z(g,1o)}:3(g){5 g.1F},getStyle:G.1n?3(g,o){4 k=G.1n.2z(g,1o);5 o x k?k[o]:k.getPropertyValue(o)}:3(g,o){4 k=g.k,1e=g.1F;9(o=="12"){9(/21\\(12=(.*)\\)/i.P(1e.1B)){4 12=parseFloat(1R.$1);5 12?12/2A:0}5 1}9(o=="2B"){o="2C"}4 p=1e[o]||1e[S.22(o)];9(!/^-?\\d+(?:px)?$/i.P(p)&&/^\\-?\\d/.P(p)){4 q=k.q,1G=g.runtimeStyle,2E=1G.q;1G.q=1e.q;k.q=p||0;p=k.pixelLeft+"px";k.q=q;1G.q=2E}5 p},23:3(1p,k,1f){9(!1p.1l){1p=[1p]}9(1L k=="2f"){4 s=k;k={};k[s]=1f}A.2o(1p,3(g){J(4 o x k){4 1f=k[o];9(o=="12"&&B.ie){g.k.1B=(g.1F&&g.1F.1B||"").2F(/21\\([^)]*\\)/,"")+" 21(12="+(1f*2A|0)+")"}N 9(o=="2B"){g.k[B.ie?"2C":"cssFloat"]=1f}N{g.k[S.22(o)]=1f}}})},getSize:3(g){4 1q=g.1X,1r=g.1Y;9(!1q&&!1r){4 24=!D.1W(G.1d,g),15;9(24){15=g.parentNode;G.1d.insertBefore(g,G.1d.childNodes[0])}4 k=g.k,2G={25:"absolute",26:"hidden",27:"block",q:"-2H",L:"-2H"},2I={25:k.25,26:k.26,27:k.27,q:k.q,L:k.L};D.23(g,2G);1q=g.1X;1r=g.1Y;D.23(g,2I);9(24){15?15.appendChild(g):G.1d.removeChild(g)}}5{"1q":1q,"1r":1r}}};E=(3(){4 1g,1h,v=1,28=3(h,f,m){9(!m.$v)m.$v=v++;9(!h.C)h.C={};4 I=h.C[f];9(!I){I=h.C[f]={};9(h["on"+f]){I[0]=h["on"+f]}}};9(1U.2a){4 1s={"mouseenter":"2J","mouseleave":"2K"};1g=3(h,f,m){9(f x 1s){28(h,f,m);4 2L=h.C[f][m.$v]=3(j){4 1H=j.1t;9(!1H||(h!=1H&&!(h.2x(1H)&16))){m.19(Z,j)}};h.2a(1s[f],2L,y)}N{h.2a(f,m,y)}};1h=3(h,f,m){9(f x 1s){9(h.C&&h.C[f]){h.2M(1s[f],h.C[f][m.$v],y);2b h.C[f][m.$v]}}N{h.2M(f,m,y)}}}N{1g=3(h,f,m){28(h,f,m);h.C[f][m.$v]=m;h["on"+f]=1I};1h=3(h,f,m){9(h.C&&h.C[f]){2b h.C[f][m.$v]}};3 1I(){4 1J=14,j=1m();4 I=Z.C[j.f];J(4 i x I){Z.$1I=I[i];9(Z.$1I(j)===y){1J=y}}5 1J}}3 1m(j){9(j)5 j;j=1U.j;j.pageX=j.clientX+D.1E(j.2c);j.pageY=j.clientY+D.1D(j.2c);j.target=j.2c;j.2d=2d;j.2e=2e;4 1t={"2K":j.toElement,"2J":j.fromElement}[j.f];9(1t){j.1t=1t}5 j};3 2d(){Z.cancelBubble=14};3 2e(){Z.1J=y};5{"1g":1g,"1h":1h,"1m":1m}})();CE=(3(){4 v=1;5{1g:3(c,f,m){9(!m.$$v)m.$$v=v++;9(!c.w)c.w={};9(!c.w[f])c.w[f]={};c.w[f][m.$$v]=m},1h:3(c,f,m){9(c.w&&c.w[f]){2b c.w[f][m.$$v]}},fireEvent:3(c,f){9(!c.w)5;4 1b=1V.17.1a.19(M,2),I=c.w[f];J(4 i x I){I[i].T(c,1b)}},clearEvent:3(c){9(!c.w)5;J(4 f x c.w){4 I=c.w[f];J(4 i x I){I[i]=1o}c.w[f]=1o}c.w=1o}}})();S={22:3(s){5 s.2F(/-([a-z])/ig,3(all,2N){5 2N.toUpperCase()})}};9(B.2j){try{G.execCommand("BackgroundImageCache",y,14)}catch(e){}};$=O;$B=B;$A=A;$F=F;$D=D;$E=E;$CE=CE;$S=S})();',[],174,'|||function|var|return||||if|||object|||type|elem|element||event|style|from|handler||name|ret|left|node||thisp|callback|guid|cusevents|in|false||||events||||document|rect|handlers|for|array|top|arguments|else||test|destination|property||apply|ua|len|elt|right|bottom|this||each|opacity|source|true|parent||prototype|msie|call|slice|args|doc|body|curStyle|value|addEvent|removeEvent||copy|version|length|fixEvent|defaultView|null|elems|width|height|fix|relatedTarget||override|vMark|indexOf|isNaN|Math|lastIndexOf|filter|fun|getScrollTop|getScrollLeft|currentStyle|rtStyle|related|handleEvent|returnValue|undefined|typeof|ins|subclass|opera|chrome|break|RegExp||parseInt|window|Array|contains|offsetWidth|offsetHeight|sLeft|sTop|alpha|camelize|setStyle|repair|position|visibility|display|storage||addEventListener|delete|srcElement|stopPropagation|preventDefault|string||safari|firefox|ie6|ie8|obj|ceil|floor|forEach|push|item|method|concat|ownerDocument|documentElement|scrollTop|scrollLeft|compareDocumentPosition|getBoundingClientRect|getComputedStyle|100|float|styleFloat||rsLeft|replace|cssShow|9999px|cssBack|mouseover|mouseout|fixhandler|removeEventListener|letter'.split('|'),0,{}));
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 = {//默认值
  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);
  }
}

调用方法如下
<input id="idFile" type="file" name="pic" /><img id="idImg" alt="" src="https://3water.com/" />
<script>
var ip = new ImagePreview( $$("idFile"), $$("idImg"), {
 maxWidth: 200, maxHeight: 2000, action: "ImagePreview.ashx"
});
ip.img.src = ImagePreview.TRANSPARENT;
ip.file.onchange = function(){ ip.preview(); };
</script>
Javascript 相关文章推荐
js或css文件后面跟参数的原因说明
Jan 09 Javascript
Javascript基础 函数“重载” 详细介绍
Oct 25 Javascript
Angular和百度地图的结合实例代码
Oct 19 Javascript
AngularJS深入探讨scope,继承结构,事件系统和生命周期
Nov 02 Javascript
详解Javascript数据类型的转换规则
Dec 12 Javascript
jQuery中 bind的用法简单介绍
Feb 13 Javascript
基于JS实现仿百度百家主页的轮播图效果
Mar 06 Javascript
详解微信小程序 登录获取unionid
Jun 27 Javascript
JavaScript设计模式之装饰者模式定义与应用示例
Jul 25 Javascript
全面了解JavaScript的作用域链
Apr 03 Javascript
JavaScript 实现同时选取多个时间段的方法
Oct 17 Javascript
微信小程序转发事件实现解析
Oct 22 Javascript
简单js代码实现selece二级联动(推荐)
Feb 18 #Javascript
超简单JS二级、多级联动的简单实例
Feb 18 #Javascript
jquery easyui中treegrid用法的简单实例
Feb 18 #Javascript
DIV始终居中的js代码
Feb 17 #Javascript
jquery实现邮箱自动补全功能示例分享
Feb 17 #Javascript
JQuery与JS里submit()的区别示例介绍
Feb 17 #Javascript
按Enter键触发事件的jquery方法实现代码
Feb 17 #Javascript
You might like
php为什么选mysql作为数据库? Mysql 创建用户方法
2007/07/02 PHP
PHP cron中的批处理
2008/09/16 PHP
需要发散思维学习PHP
2009/06/29 PHP
使用php+Ajax实现唯一校验实现代码[简单应用]
2011/11/29 PHP
php实现文件下载功能的几个代码分享
2014/05/10 PHP
使用新浪微博API的OAuth认证发布微博实例
2015/03/27 PHP
win7系统配置php+Apache+mysql环境的方法
2015/08/21 PHP
thinkphp命名空间用法实例详解
2015/12/30 PHP
Laravel 之url参数,获取路由参数的例子
2019/10/21 PHP
Textbox控件注册回车事件及触发按钮提交事件具体实现
2013/03/04 Javascript
javascript得到当前页的来路即前一页地址的方法
2014/02/18 Javascript
JS中判断JSON数据是否存在某字段的方法
2014/03/07 Javascript
jquery选择符快速提取web表单数据示例
2014/03/27 Javascript
js获取当前页面的url网址信息
2014/06/12 Javascript
用jquery模仿的a的title属性的例子
2014/10/22 Javascript
jQuery实现新消息闪烁标题提示的方法
2015/03/11 Javascript
jquery使用animate方法实现控制元素移动
2015/03/27 Javascript
基于JS代码实现实时显示系统时间
2016/06/16 Javascript
js删除局部变量的实现方法
2016/06/25 Javascript
vue2.0结合DataTable插件实现表格动态刷新的方法详解
2017/03/17 Javascript
Vue.js 使用v-cloak后仍显示变量的解决方法
2018/11/19 Javascript
微信小程序自定义可滑动顶部TabBar选项卡实现页面切换功能示例
2019/05/14 Javascript
详解iview的checkbox多选框全选时校验问题
2019/06/10 Javascript
微信小程序request请求封装,验签代码实例
2019/12/04 Javascript
[03:08]迎霜节狂欢!2018年迎霜节珍藏Ⅰ一览
2018/12/25 DOTA
Python实现查找二叉搜索树第k大的节点功能示例
2019/01/24 Python
python区分不同数据类型的方法
2019/10/14 Python
python实现密码强度校验
2020/03/18 Python
基于css3实现漂亮便签样式
2013/03/18 HTML / CSS
html5教你做炫酷的碎片式图片切换 (canvas)
2017/07/28 HTML / CSS
Timberland美国官网:全球领先的户外品牌
2016/08/15 全球购物
如何保障Web服务器安全
2014/05/05 面试题
介绍一下你对SOA的认识
2016/04/24 面试题
3分钟英语演讲稿
2014/04/29 职场文书
2016年春节慰问信息大全
2015/11/30 职场文书
如何用vue实现网页截图你知道吗
2021/11/17 Vue.js