首页 > web前端 > js教程 > 正文

图片上传即时显示缩略图的js代码_图象特效

WBOY
发布: 2016-05-16 18:52:21
原创
977 人浏览过
<<SPAN class=start-tag>script</SPAN><SPAN class=attribute-name> language</SPAN>=<SPAN class=attribute-value>"javascript" </SPAN><SPAN class=attribute-name>type</SPAN>=<SPAN class=attribute-value>"text/javascript"</SPAN>><br> var allowExt = ['jpg', 'gif', 'bmp', 'png', 'jpeg'];<br> var preivew = function(file, container){<br>  try{<br>  var pic = new Picture(file, container);<br>  }catch(e){<br>  alert(e);<br>  }<br> }<br> //缩略图类定义<br>   var Picture = function(file, container){<br>  var height = 0, <br>  widht = 0, <br>  ext = '',<br>  size = 0,<br>  name  = '',<br>  path  = '';<br>  var self = this;<br>  if(file){ <br>    name = file.value;<br>  if (window.navigator.userAgent.indexOf("MSIE")>=1){ <br>   file.select(); <br>   path = document.selection.createRange().text; <br>  }else if(window.navigator.userAgent.indexOf("Firefox")>=1){ <br>   if(file.files){ <br>   path = file.files.item(0).getAsDataURL(); <br>   }else{<br>   path = file.value;<br>   }<br>  } <br>  }else{<br>  throw "bad file";<br>  } <br><br><br>  ext = name.substr(name.lastIndexOf("."), name.length);<br><br>  if(container.tagName.toLowerCase() != 'img'){<br>  throw "container is not a valid img label";<br>  container.visibility = 'hidden';<br>  }<br>  container.src = path;<br>  container.alt = name;<br>  container.style.visibility = 'visible';<br>  height = container.height;<br>  widht = container.widht;<br>  size  = container.fileSize;<br><br><br>  this.get = function(name){<br>  return self[name];<br>  }<br><br>  this.isValid = function(){<br>  if(allowExt.indexOf(self.ext) !== -1){<br>   throw 'the ext is not allowed to upload';<br>   return false;<br>  }<br>  }<br> }<br></<SPAN class=end-tag>script</SPAN>><br>  <<SPAN class=start-tag>div</SPAN><SPAN class=attribute-name> class</SPAN>=<SPAN class=attribute-value>'previewDemo'</SPAN>><br> <<SPAN class=start-tag>input</SPAN><SPAN class=attribute-name> id</SPAN>=<SPAN class=attribute-value>"file" </SPAN><SPAN class=attribute-name>type</SPAN>=<SPAN class=attribute-value>"file" </SPAN><SPAN class=attribute-name>onchange</SPAN>=<SPAN class=attribute-value>"preivew(this, document.getElementById('img'));"</SPAN>><br> <<SPAN class=start-tag>img</SPAN><SPAN class=attribute-name> id</SPAN>=<SPAN class=attribute-value>"img" </SPAN><SPAN class=attribute-name>style</SPAN>=<SPAN class=attribute-value>"visibility:hidden" </SPAN><SPAN class=attribute-name>height</SPAN>=<SPAN class=attribute-value>"100px" </SPAN><SPAN class=attribute-name>width</SPAN>=<SPAN class=attribute-value>"100px"</SPAN>><br></<SPAN class=end-tag>div</SPAN>>
登录后复制
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板