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 相关文章推荐
捕获键盘事件(且兼容各浏览器)
Jul 03 Javascript
Extjs NumberField后面加单位实现思路
Jul 30 Javascript
12种JavaScript常用的MVC框架比较分析
Nov 16 Javascript
jQuery siblings()用法实例详解
Apr 26 Javascript
jQuery通过ajax请求php遍历json数组到table中的代码(推荐)
Jun 12 Javascript
JS正则表达式修饰符global(/g)用法分析
Dec 27 Javascript
详解微信小程序实现跑马灯效果(附完整代码)
Apr 29 Javascript
vue 实现滚动到底部翻页效果(pc端)
Jul 31 Javascript
Vue的编码技巧与规范使用详解
Aug 28 Javascript
浅谈layui 绑定form submit提交表单的注意事项
Oct 25 Javascript
JS组件库AlloyTouch实现图片轮播过程解析
May 29 Javascript
纯js+css实现在线时钟
Aug 18 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
PHP文本操作类
2006/11/25 PHP
PHP json_encode中文乱码问题的解决办法
2013/09/09 PHP
php 解析xml 的四种方法详细介绍
2016/10/26 PHP
PHP与JavaScript针对Cookie的读写、交互操作方法详解
2017/08/07 PHP
告诉你什么是javascript的回调函数
2014/09/04 Javascript
JavaScript学习心得之概述
2015/01/20 Javascript
onclick和onblur冲突问题的快速解决方法
2016/04/28 Javascript
微信小程序开发之入门实例教程篇
2017/03/07 Javascript
js实现瀑布流效果(自动生成新的内容)
2017/03/16 Javascript
浅谈angularJS的$watch失效问题的解决方案
2017/08/11 Javascript
js 两个日期比较相差多少天的实例
2017/10/19 Javascript
关于JavaScript语句后面的分号问题
2017/12/07 Javascript
Angular @HostBinding()和@HostListener()用法
2018/03/05 Javascript
vue全局自定义指令-元素拖拽的实现代码
2019/04/14 Javascript
微信小程序商品详情页底部弹出框
2019/11/22 Javascript
python3编写C/S网络程序实例教程
2014/08/25 Python
实例Python处理XML文件的方法
2015/08/31 Python
python 排序算法总结及实例详解
2016/09/28 Python
深入理解NumPy简明教程---数组3(组合)
2016/12/17 Python
python 实现对文件夹中的图像连续重命名方法
2018/10/25 Python
对python3 sort sorted 函数的应用详解
2019/06/27 Python
Python之Numpy的超实用基础详细教程
2019/10/23 Python
python Popen 获取输出,等待运行完成示例
2019/12/30 Python
在pycharm中debug 实时查看数据操作(交互式)
2020/06/09 Python
对CSS3选择器的研究(详解)
2016/09/16 HTML / CSS
html5指南-1.html5全局属性(html5 global attributes)深入理解
2013/01/07 HTML / CSS
html5中嵌入视频自动播放的问题解决
2020/05/25 HTML / CSS
澳大利亚领先的在线机械五金、园艺和存储专家:Edisons
2018/03/24 全球购物
介绍一下Prototype的$()函数,$F()函数,$A()函数都是什么作用?
2014/03/05 面试题
英语专业应届生求职信范文
2013/11/15 职场文书
硕士生工作推荐信
2014/03/07 职场文书
立志成才演讲稿
2014/09/04 职场文书
2014年店长工作总结
2014/11/17 职场文书
2015新年联欢晚会开场白
2014/12/14 职场文书
学生会招新宣传语
2015/07/13 职场文书
Redis基本数据类型Set常用操作命令
2022/06/01 Redis