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 相关文章推荐
Firefox div高度自适应
Apr 28 Javascript
学习ExtJS accordion布局
Oct 08 Javascript
javascript 实现简单的table排序及table操作练习
Dec 28 Javascript
一个Action如何调用两个不同的方法
May 22 Javascript
js事件监听机制(事件捕获)总结
Aug 08 Javascript
jquery实现从数组移除指定的值
Jun 24 Javascript
JS实现选择TextArea内文本的方法
Aug 03 Javascript
js检测iframe是否加载完成的方法
Nov 26 Javascript
vue上传图片组件编写代码
Jul 26 Javascript
微信小程序-滚动消息通知的实例代码
Aug 03 Javascript
浅谈vue-lazyload实现的详细过程
Aug 22 Javascript
vue将对象新增的属性添加到检测序列的方法
Feb 24 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基础知识:类与对象(2) 自动加载对象
2006/12/13 PHP
php session 错误
2009/05/21 PHP
Ajax+PHP 边学边练之四 表单
2009/11/27 PHP
php 仿Comsenz安装效果代码打包提供下载
2010/05/09 PHP
php checkbox复选框值的获取与checkbox默认值输出方法
2010/05/15 PHP
使用php+Ajax实现唯一校验实现代码[简单应用]
2011/11/29 PHP
PHP ? EasyUI DataGrid 资料存的方式介绍
2012/11/07 PHP
win7 64位系统 配置php最新版开发环境(php+Apache+mysql)
2014/08/15 PHP
PHP中SESSION的注销与清除
2015/04/16 PHP
php获取数据库结果集方法(推荐)
2017/06/01 PHP
PHP文件类型检查及fileinfo模块安装使用详解
2019/05/09 PHP
php文件上传原理与实现方法详解
2019/12/20 PHP
Javascript模块模式分析
2008/05/16 Javascript
Javascript hasOwnProperty 方法 &amp; in 关键字
2008/11/26 Javascript
jQuery Ajax调用WCF服务详细教程
2015/03/31 Javascript
JavaScript检测字符串中是否含有html标签实现方法
2015/07/01 Javascript
深入浅析JavaScript中prototype和proto的关系
2015/11/15 Javascript
JS、jQuery中select的用法详解
2016/04/21 Javascript
jQuery 弹出层插件(推荐)
2016/05/24 Javascript
Ionic2系列之使用DeepLinker实现指定页面URL
2016/11/21 Javascript
微信小程序开发(二)图片上传+服务端接收详解
2017/01/11 Javascript
详解微信小程序中组件通讯
2018/10/30 Javascript
KOA+egg.js集成kafka消息队列的示例
2018/11/09 Javascript
vue使用微信扫一扫功能的实现代码
2020/04/11 Javascript
微信小程序语音同步智能识别的实现案例代码解析
2020/05/29 Javascript
基于vuex实现购物车功能
2021/01/10 Vue.js
Python 读取串口数据,动态绘图的示例
2019/07/02 Python
python读取.mat文件的数据及实例代码
2019/07/12 Python
Python使用指定字符长度切分数据示例
2019/12/05 Python
Canvas图片分割效果的实现
2019/07/29 HTML / CSS
高尔夫球鞋、服装、手套和装备:FootJoy
2018/12/15 全球购物
美国翻新电子产品商店:The Store
2019/10/08 全球购物
春节联欢晚会主持词范文
2014/03/24 职场文书
房屋公证委托书
2014/04/03 职场文书
Win11显卡控制面板打开显卡设置方法
2022/04/20 数码科技
Golang 入门 之url 包
2022/05/04 Golang