javascript实现input file上传图片预览效果


Posted in Javascript onDecember 31, 2015

本文实例介绍了javascript实现input file上传图片预览效果的详细代码,分享给大家供大家参考,具体内容如下

运行效果图:

javascript实现input file上传图片预览效果

具体实现代码:

<!DOCTYPE html>
<html>

<head>
 <meta charset="utf-8">
 <title></title>
 <script type="text/javascript" src="jquery-1.11.1.min.js"></script>

 <style type="text/css">
  .imgbox,.imgbox1
  {
   float: left;
   margin-right: 20px;
   margin-top: 20px;
   position: relative;
   width: 182px;
   height: 142px;
   border: 1px solid red;
   overflow: hidden;
  }
  .imgbox1{border: 1px solid blue;
  }


  .imgnum{
   left: 0px;
   top: 0px;
   margin: 0px;
   padding: 0px;
  }
  .imgnum input,.imgnum1 input {
   position: absolute;
   width: 182px;
   height: 142px;
   opacity: 0;
  }
  .imgnum img,.imgnum1 img {
   width: 100%;
   height: 100%;
  }
  .close,
  .close1 {
   color: red;
   position: absolute;
   left: 170px;
   top: 0px;
   display: none;
  }





 </style>
</head>

<body>
<div id="img">
<div class="imgbox">
 <div class="imgnum">
  <input type="file" class="filepath" />
  <span class="close">X</span>
  <img src="btn.png" class="img1" />
  <img src="" class="img2" />
 </div>
</div><div class="imgbox">
 <div class="imgnum">
  <input type="file" class="filepath" />
  <span class="close">X</span>
  <img src="btn.png" class="img1" />
  <img src="" class="img2" />
 </div>
</div>
<div class="imgbox">
 <div class="imgnum">
  <input type="file" class="filepath" />
  <span class="close">X</span>
  <img src="btn.png" class="img1" />
  <img src="" class="img2" />
 </div>
</div>
<div class="imgbox">
 <div class="imgnum">
  <input type="file" class="filepath" />
  <span class="close">X</span>
  <img src="btn.png" class="img1" />
  <img src="" class="img2" />
 </div>
</div>
<div class="imgbox">
 <div class="imgnum">
  <input type="file" class="filepath" />
  <span class="close">X</span>
  <img src="btn.png" class="img1" />
  <img src="" class="img2" />
 </div>
</div>
<div class="imgbox">
 <div class="imgnum">
  <input type="file" class="filepath" />
  <span class="close">X</span>
  <img src="btn.png" class="img1" />
  <img src="" class="img2" />
 </div>
</div>
<div class="imgbox">
 <div class="imgnum">
  <input type="file" class="filepath" />
  <span class="close">X</span>
  <img src="btn.png" class="img1" />
  <img src="" class="img2" />
 </div>
</div>
<div class="imgbox">
 <div class="imgnum">
  <input type="file" class="filepath" />
  <span class="close">X</span>
  <img src="btn.png" class="img1" />
  <img src="" class="img2" />
 </div>
</div> <div class="imgbox">
 <div class="imgnum">
  <input type="file" class="filepath" />
  <span class="close">X</span>
  <img src="btn.png" class="img1" />
  <img src="" class="img2" />
 </div>
</div>



<div class="imgbox1">
 <div class="imgnum">
  <input type="file" class="filepath1" />
  <span class="close1">X</span>
  <img src="btn.png" class="img11" />
  <img src="" class="img22" />
 </div>
</div>

</div>

</body>
<script type="text/javascript">
 $(function() {
  $(".filepath").on("change",function() {
   alert($('.imgbox').length);
   var srcs = getObjectURL(this.files[0]); //获取路径
   $(this).nextAll(".img1").hide(); //this指的是input
   $(this).nextAll(".img2").show(); //fireBUg查看第二次换图片不起做用
   $(this).nextAll('.close').show(); //this指的是input
   $(this).nextAll(".img2").attr("src",srcs); //this指的是input
   $(this).val(''); //必须制空
   $(".close").on("click",function() {
    $(this).hide();  //this指的是span
    $(this).nextAll(".img2").hide();
    $(this).nextAll(".img1").show();
   })
  })
 })




 function getObjectURL(file) {
  var url = null;
  if (window.createObjectURL != undefined) {
   url = window.createObjectURL(file)
  } else if (window.URL != undefined) {
   url = window.URL.createObjectURL(file)
  } else if (window.webkitURL != undefined) {
   url = window.webkitURL.createObjectURL(file)
  }
  return url
 };





 $(function() {
  $("#img").on("change",".filepath1",function() {
   //alert($('.imgbox1').length);
   var srcs = getObjectURL(this.files[0]); //获取路径
   alert(srcs);
   //this指的是input
   /* $(this).nextAll(".img22").attr("src",srcs); //this指的是input
    $(this).nextAll(".img22").show(); //fireBUg查看第二次换图片不起做用*/
   var htmlImg='<div class="imgbox1">'+
     '<div class="imgnum1">'+
     '<input type="file" class="filepath1" />'+
     '<span class="close1">X</span>'+
     '<img src="btn.png" class="img11" />'+
     '<img src="'+srcs+'" class="img22" />'+
     '</div>'+
     '</div>';

   $(this).parent().parent().before(htmlImg);
   $(this).val(''); //必须制空
   $(this).parent().parent().prev().find(".img11").hide(); //this指的是input
   $(this).parent().parent().prev().find('.close1').show();

   $(".close1").on("click",function() {
    $(this).hide();  //this指的是span
    $(this).nextAll(".img22").hide();
    $(this).nextAll(".img11").show();
    if($('.imgbox1').length>1){
     $(this).parent().parent().remove();
    }

   })
  })
 })

</script>

</html>

希望本文所述对大家学习javascript程序设计有所帮助。

Javascript 相关文章推荐
js 跨域和ajax 跨域问题小结
Jul 01 Javascript
JavaScript 设计模式之组合模式解析
Apr 09 Javascript
jQuery中 prop() attr()使用详解
May 19 Javascript
window.onerror()的用法与实例分析
Jan 27 Javascript
JS实现的颜色实时渐变效果完整实例
Mar 25 Javascript
jQuery插件扩展实例【添加回调函数】
Nov 26 Javascript
解决IE7中使用jQuery动态操作name问题
Aug 28 jQuery
JS运动特效之完美运动框架实例分析
Jan 24 Javascript
checkbox在vue中的用法小结
Nov 13 Javascript
JS获取动态添加元素的方法详解
Jul 31 Javascript
vue-dplayer 视频播放器实例代码
Nov 08 Javascript
JavaScript 浏览器对象模型BOM原理与常见用法实例分析
Dec 16 Javascript
分享几种比较简单实用的JavaScript tabel切换
Dec 31 #Javascript
jQuery+ajax实现文章点赞功能的方法
Dec 31 #Javascript
jQuery实现的超简单点赞效果实例分析
Dec 31 #Javascript
jQuery实现的给图片点赞+1动画效果(附在线演示及demo源码下载)
Dec 31 #Javascript
jQuery实现的点赞随机数字显示动画效果(附在线演示与demo源码下载)
Dec 31 #Javascript
AngularJS中实现显示或隐藏动画效果的方式总结
Dec 31 #Javascript
javascript数据类型验证方法
Dec 31 #Javascript
You might like
使用XDebug调试及单元测试覆盖率分析
2011/01/27 PHP
通过PHP的内置函数,通过DES算法对数据加密和解密
2012/06/21 PHP
TP5框架实现签到功能的方法分析
2020/04/05 PHP
js+FSO遍历文件夹下文件并显示
2007/03/07 Javascript
js 替换
2008/02/19 Javascript
XENON基于JSON变种
2010/07/27 Javascript
获取内联和链接中的样式(js代码)
2013/04/11 Javascript
JQuery获取或设置ckeditor的数据(示例代码)
2013/11/15 Javascript
解决自定义$(id)的方法与jquery选择器$冲突的问题
2014/06/14 Javascript
微信小程序 教程之WXML
2016/10/18 Javascript
JS跳转手机站url的若干注意事项
2017/10/18 Javascript
微信小程序使用radio显示单选项功能【附源码下载】
2017/12/11 Javascript
vue组件与复用详解
2018/04/08 Javascript
JavaScript实现计算圆周率到小数点后100位的方法示例
2018/05/08 Javascript
微信小程序仿美团城市选择
2018/06/06 Javascript
vue实现点击隐藏与显示实例分享
2019/02/13 Javascript
node.js实现带进度条的多文件上传
2020/03/27 Javascript
python正则表达式抓取成语网站
2013/11/20 Python
python用Pygal如何生成漂亮的SVG图像详解
2017/02/10 Python
Golang与python线程详解及简单实例
2017/04/27 Python
Python内置模块logging用法实例分析
2018/02/12 Python
python如何将图片转换为字符图片
2020/08/19 Python
详解Python正则表达式re模块
2019/03/19 Python
Python MongoDB 插入数据时已存在则不执行,不存在则插入的解决方法
2019/09/24 Python
Python +Selenium解决图片验证码登录或注册问题(推荐)
2020/02/09 Python
浅谈Python的方法解析顺序(MRO)
2020/03/05 Python
CSS3 Pie工具推荐--让IE6-8支持一些优秀的CSS3特性
2014/09/02 HTML / CSS
用HTML5制作烟火效果的教程
2015/05/12 HTML / CSS
Urban Decay官方网站:美国化妆品品牌
2020/06/04 全球购物
施工资料员岗位职责
2014/01/06 职场文书
退休感言
2014/01/28 职场文书
文明美德伴我成长演讲稿
2014/05/12 职场文书
公证委托书
2014/08/01 职场文书
干部四风问题整改措施思想汇报
2014/10/13 职场文书
毕业实习单位意见
2015/06/04 职场文书
管理者们如何制定2019年的工作计划?
2019/07/01 职场文书