JS实现的input选择图片本地预览功能示例


Posted in Javascript onAugust 29, 2018

本文实例讲述了JS实现的input选择图片本地预览功能。分享给大家供大家参考,具体如下:

预览效果见下图:

JS实现的input选择图片本地预览功能示例

HTML代码如下:

<div class="content" style="margin-top:100px;height:200px;">
  <div id="div4bm" style="float:left;">
  <!--input[button] 触发 file click事件-->
  <input type="button" value="选择文件" id="mybutton" class="mybtn" onclick="Id('file').click();" />
  <!--file 隐藏起来 触发onchange事件-->
  <input type="file" name="file" accept="image/png,image/jpg,image/jpeg" id="file" onchange="changeToop();" style="display:none;" />
  </div>
  <!--图片展示区域-->
  <div style="float:left;">
    <!--设置默认图片-->
    <img id="myimg" src="http://sandbox.runjs.cn/uploads/rs/72/huvtowwn/zanwu.png"/>
</div>

CSS代码如下:

.mybtn{
  width:100px;
  height:30px;
  display:inline-block;
  background-color:rgb(91,183,91);
  border:1px solid rgb(91,183,91);
  border-radius:3px;
  color:white;
  font-size:14px;
  font-family:微软雅黑;
  cursor:pointer;
  text-align:center;
  vertical-align: center;
  box-shadow:0px 0px 1px 1px rgb(91,160,91);
}
.mybtn:hover{
  background-color:rgb(91,160,91);
  border-color:rgb(91,160,91);
  color:white;
  text-decoration:none;
}
.myinp{
  width:100px;
  height:30px;
  display:inline-block;
  border:1px solid rgb(209,232,250);
  border-radius:2px;
}
#div4bm{
  padding-top:15px;
  margin-right:15px;
  }
  #mybutton{
  margin-left:100px;
}
#myimg{
  width:164px;
  height:164px;
}

JS代码如下:

//定义id选择器
function Id(id){
return document.getElementById(id);
}
function changeToop(){
  var file = Id("file");
  if(file.value==''){
    //设置默认图片
  Id("myimg").src='http://sandbox.runjs.cn/uploads/rs/72/huvtowwn/zanwu.png';
  }else{
    preImg("file","myimg");
  }
}
//获取input[file]图片的url Important
function getFileUrl(fileId) {
  var url;
  var file = Id(fileId);
  var agent = navigator.userAgent;
  if (agent.indexOf("MSIE")>=1) {
  url = file.value;
  } else if(agent.indexOf("Firefox")>0) {
  url = window.URL.createObjectURL(file.files.item(0));
  } else if(agent.indexOf("Chrome")>0) {
  url = window.URL.createObjectURL(file.files.item(0));
  }
  return url;
}
//读取图片后预览
function preImg(fileId,imgId) {
var imgPre =Id(imgId);
imgPre.src = getFileUrl(fileId);
}

上述代码可使用在线HTML/CSS/JavaScript前端代码调试运行工具:http://tools.3water.com/code/WebCodeRun测试运行效果。

也可使用在线HTML/CSS/JavaScript代码运行工具:http://tools.3water.com/code/HtmlJsRun运行如下完整代码得到上面图示效果。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>3water.com js input选择图片本地预览</title>
<style>
.mybtn{
  width:100px;
  height:30px;
  display:inline-block;
  background-color:rgb(91,183,91);
  border:1px solid rgb(91,183,91);
  border-radius:3px;
  color:white;
  font-size:14px;
  font-family:微软雅黑;
  cursor:pointer;
  text-align:center;
  vertical-align: center;
  box-shadow:0px 0px 1px 1px rgb(91,160,91);
}
.mybtn:hover{
  background-color:rgb(91,160,91);
  border-color:rgb(91,160,91);
  color:white;
  text-decoration:none;
}
.myinp{
  width:100px;
  height:30px;
  display:inline-block;
  border:1px solid rgb(209,232,250);
  border-radius:2px;
}
#div4bm{
  padding-top:15px;
  margin-right:15px;
  }
  #mybutton{
  margin-left:100px;
}
#myimg{
  width:164px;
  height:164px;
}
</style>
</head>
<body>
<div class="content" style="margin-top:100px;height:200px;">
  <div id="div4bm" style="float:left;">
  <!--input[button] 触发 file click事件-->
  <input type="button" value="选择文件" id="mybutton" class="mybtn" onclick="Id('file').click();" />
  <!--file 隐藏起来 触发onchange事件-->
  <input type="file" name="file" accept="image/png,image/jpg,image/jpeg" id="file" onchange="changeToop();" style="display:none;" />
  </div>
  <!--图片展示区域-->
  <div style="float:left;">
    <!--设置默认图片-->
    <img id="myimg" src="http://sandbox.runjs.cn/uploads/rs/72/huvtowwn/zanwu.png"/>
</div>
<script>
//定义id选择器
function Id(id){
return document.getElementById(id);
}
function changeToop(){
  var file = Id("file");
  if(file.value==''){
    //设置默认图片
  Id("myimg").src='http://sandbox.runjs.cn/uploads/rs/72/huvtowwn/zanwu.png';
  }else{
    preImg("file","myimg");
  }
}
//获取input[file]图片的url Important
function getFileUrl(fileId) {
  var url;
  var file = Id(fileId);
  var agent = navigator.userAgent;
  if (agent.indexOf("MSIE")>=1) {
  url = file.value;
  } else if(agent.indexOf("Firefox")>0) {
  url = window.URL.createObjectURL(file.files.item(0));
  } else if(agent.indexOf("Chrome")>0) {
  url = window.URL.createObjectURL(file.files.item(0));
  }
  return url;
}
//读取图片后预览
function preImg(fileId,imgId) {
var imgPre =Id(imgId);
imgPre.src = getFileUrl(fileId);
}
</script>
</body>
</html>

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

Javascript 相关文章推荐
javascript之对系统的toFixed()方法的修正
May 08 Javascript
Javascript+XMLHttpRequest+asp.net无刷新读取数据库数据
Aug 09 Javascript
js 获取中文拼音,Select自动匹配字母获取值的代码
Sep 23 Javascript
Jquery判断$(&quot;#id&quot;)获取的对象是否存在的方法
Sep 25 Javascript
jquery获取URL中参数解决中文乱码问题的两种方法
Dec 18 Javascript
js中日期的加减法
May 06 Javascript
SWFObject基本用法实例分析
Jul 20 Javascript
jquery实现浮动在网页右下角的彩票开奖公告窗口代码
Sep 04 Javascript
值得收藏的vuejs安装教程
Nov 21 Javascript
jquery实现动态添加附件功能
Oct 23 jQuery
详解CommonJS和ES6模块循环加载处理的区别
Dec 26 Javascript
解决echarts图表使用v-show控制图表显示不全的问题
Jul 19 Javascript
简述vue状态管理模式之vuex
Aug 29 #Javascript
jQuery实现的简单手风琴效果示例
Aug 29 #jQuery
angularjs实现对表单输入改变的监控(ng-change和watch两种方式)
Aug 29 #Javascript
jQuery实现的淡入淡出图片轮播效果示例
Aug 29 #jQuery
JS实现数组的增删改查操作示例
Aug 29 #Javascript
vue-video-player 通过自定义按钮组件实现全屏切换效果【推荐】
Aug 29 #Javascript
vue-cli3.0使用及部分配置详解
Aug 29 #Javascript
You might like
php 过滤英文标点符号及过滤中文标点符号代码
2014/06/12 PHP
详细解读php的命名空间(二)
2018/02/21 PHP
PHP使用 Pear 进行安装和卸载包的方法详解
2019/07/08 PHP
解决PHP使用CURL发送GET请求时传递参数的问题
2019/10/11 PHP
添加到收藏夹代码(兼容几乎所有的浏览器)
2007/01/09 Javascript
js获取TreeView控件选中节点的Text和Value值的方法
2012/11/24 Javascript
原生js拖拽(第一课 未兼容)拖拽思路
2013/03/29 Javascript
js使浏览器窗口最大化实现代码(适用于IE)
2013/08/07 Javascript
用jquery中插件dialog实现弹框效果实例代码
2013/11/15 Javascript
JS在可编辑的div中的光标位置插入内容的方法
2014/11/20 Javascript
JavaScript子窗口调用父窗口变量和函数的方法
2015/10/09 Javascript
详解Bootstrap的aria-label和aria-labelledby应用
2016/01/04 Javascript
JavaScript中数组Array.sort()排序方法详解
2017/03/01 Javascript
underscore之Collections_动力节点Java学院整理
2017/07/10 Javascript
Vue的移动端多图上传插件vue-easy-uploader的示例代码
2017/11/27 Javascript
Vue 技巧之控制父类的 slot
2020/02/24 Javascript
浅谈JavaScript中的“!!”作用
2020/08/03 Javascript
angular *Ngif else用法详解
2020/12/15 Javascript
[56:01]2018DOTA2亚洲邀请赛 3.31 小组赛 B组 Effect vs EG
2018/03/31 DOTA
python遍历文件夹下所有excel文件
2018/01/03 Python
python开发准备工作之配置虚拟环境(非常重要)
2019/02/11 Python
python实现图像检索的三种(直方图/OpenCV/哈希法)
2019/08/08 Python
使用python将最新的测试报告以附件的形式发到指定邮箱
2019/09/20 Python
python multiprocessing多进程变量共享与加锁的实现
2019/10/02 Python
手把手教你进行Python虚拟环境配置教程
2020/02/03 Python
Python 随机按键模拟2小时
2020/12/30 Python
好矿嫂事迹材料
2014/01/21 职场文书
物业经理自我鉴定
2014/03/03 职场文书
学校出纳员岗位职责
2014/03/18 职场文书
我的老师教学反思
2014/05/01 职场文书
生日宴会策划方案
2014/06/03 职场文书
党员个人公开承诺书
2014/08/29 职场文书
挂职学习心得体会
2014/09/09 职场文书
毕业实习证明(4篇)
2014/10/28 职场文书
python基于tkinter实现gif录屏功能
2021/05/19 Python
修改并编译golang源码的操作步骤
2021/07/25 Golang