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 相关文章推荐
用apply让javascript函数仅执行一次的代码
Jun 27 Javascript
jquery scroll()区分横向纵向滚动条的方法
Apr 04 Javascript
JS实现霓虹灯文字效果的方法
Aug 06 Javascript
基于jQuery实现响应式圆形图片轮播特效
Nov 25 Javascript
精通JavaScript的this关键字
May 28 Javascript
实例详解ECMAScript5中新增的Array方法
Apr 05 Javascript
Angular.JS中的指令引用template与指令当做属性详解
Mar 30 Javascript
vue组件中使用props传递数据的实例详解
Apr 08 Javascript
angular4笔记系列之内置指令小结
Nov 09 Javascript
Node.js创建一个Express服务的方法详解
Jan 06 Javascript
JavaScript实现省市区三级联动
Feb 13 Javascript
node.js中module模块的功能理解与用法实例分析
Feb 14 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
Ha0k 0.3 PHP 网页木马修改版
2009/10/11 PHP
PHP命令空间namespace及use的用法小结
2017/11/27 PHP
自动生成文章摘要的代码[JavaScript 版本]
2007/03/20 Javascript
CSS和JS标签style属性对照表(方便js开发的朋友)
2010/11/11 Javascript
jQuery cdn使用介绍
2013/05/08 Javascript
复选框全选与全不选操作实现思路
2013/08/18 Javascript
js中用window.open()打开多个窗口的name问题
2014/03/13 Javascript
深入理解javascript的执行顺序
2014/04/04 Javascript
使用nodejs开发cli项目实例
2015/06/03 NodeJs
jQuery打字效果实现方法(附demo源码下载)
2015/12/18 Javascript
JSP防止网页刷新重复提交数据的几种方法
2016/11/19 Javascript
Three.js实现浏览器变动时进行自适应的方法
2017/09/26 Javascript
js实现控制文件拖拽并获取拖拽内容功能
2018/02/17 Javascript
antd Upload 文件上传的示例代码
2018/12/14 Javascript
jQuery使用$.extend(true,object1, object2);实现深拷贝对象的方法分析
2019/03/06 jQuery
vue 中url 链接左边的小图标更改问题
2019/12/30 Javascript
[06:50]DSPL次级职业联赛十强晋级之路
2014/11/18 DOTA
Python中lambda的用法及其与def的区别解析
2014/07/28 Python
python二进制文件的转译详解
2019/07/03 Python
TensorFlow2.X使用图片制作简单的数据集训练模型
2020/04/08 Python
Python 中如何写注释
2020/08/28 Python
python自动化测试三部曲之unittest框架的实现
2020/10/07 Python
泰海淘:泰国king Power王权免税集团旗下跨境海淘综合型电商
2020/07/26 全球购物
简述进程的启动、终止的方式以及如何进行进程的查看
2013/07/12 面试题
工程力学专业毕业生求职信
2013/10/06 职场文书
餐饮投资计划书
2014/04/25 职场文书
工厂车间标语
2014/06/19 职场文书
新文化运动的基本口号
2014/06/21 职场文书
个人四风对照检查材料
2014/09/26 职场文书
关于运动会广播稿200字
2014/10/08 职场文书
2014年会计工作总结
2014/11/27 职场文书
2014年学校体育工作总结
2014/12/08 职场文书
研究生导师评语
2014/12/31 职场文书
个人借条范本
2015/05/25 职场文书
会议室管理制度范本
2015/08/06 职场文书
MySQL 5.7常见数据类型
2021/07/15 MySQL