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 相关文章推荐
JS返回上一页实例代码通过图片和按钮分别实现
Aug 16 Javascript
js+css 实现遮罩居中弹出层(随浏览器窗口滚动条滚动)
Dec 11 Javascript
js实现身份证号码验证的简单实例
Feb 19 Javascript
基于vuejs+webpack的日期选择插件
May 21 Javascript
angularJS利用ng-repeat遍历二维数组的实例代码
Jun 03 Javascript
Vue中定义全局变量与常量的各种方式详解
Aug 23 Javascript
基于vue v-for 多层循环嵌套获取行数的方法
Sep 26 Javascript
vue-cli3环境变量与分环境打包的方法示例
Feb 18 Javascript
Vue.js特性Scoped Slots的浅析
Feb 20 Javascript
Layer组件多个iframe弹出层打开与关闭及参数传递的方法
Sep 25 Javascript
原生js实现瀑布流效果
Mar 09 Javascript
js实现简单的倒计时
Jan 28 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
教你IIS6的PHP最佳配置方法
2006/09/05 PHP
浅析PHP水印技术
2007/02/14 PHP
PHP防止图片盗用(盗链)的方法小结
2016/11/11 PHP
浅谈PHP中try{}catch{}的使用方法
2016/12/09 PHP
TP3.2框架分页相关实现方法分析
2020/06/03 PHP
jquery 获取json数据实现代码
2009/04/27 Javascript
javascript定时保存表单数据的代码
2011/03/17 Javascript
修复IE9&amp;safari 的sort方法
2011/10/21 Javascript
javascript测试题练习代码
2012/10/10 Javascript
js中生成map对象的方法
2014/01/09 Javascript
ANGULARJS中使用JQUERY分页控件
2015/09/16 Javascript
Underscore源码分析
2015/12/30 Javascript
基于js中的原型(全面讲解)
2017/09/19 Javascript
AngularJS实现表单元素值绑定操作示例
2017/10/11 Javascript
JavaScript解析JSON数据示例
2019/07/16 Javascript
微信小程序实现抖音播放效果的实例代码
2020/04/11 Javascript
详解JavaScript中的链式调用
2020/11/27 Javascript
[52:07]完美世界DOTA2联赛PWL S3 LBZS vs access 第二场 12.10
2020/12/13 DOTA
全面解读Python Web开发框架Django
2014/06/30 Python
Python中的pprint折腾记
2015/01/21 Python
Python中非常实用的一些功能和函数分享
2015/02/14 Python
Python字符串和文件操作常用函数分析
2015/04/08 Python
python 编程之twisted详解及简单实例
2017/01/28 Python
Python3实现统计单词表中每个字母出现频率的方法示例
2019/01/28 Python
Python实现括号匹配方法详解
2020/02/10 Python
详解纯CSS3制作的20种loading动效
2017/07/05 HTML / CSS
比利时买床:Beter Bed
2017/12/06 全球购物
委托与事件是什么关系?为什么要使用委托
2014/04/18 面试题
外语专业毕业生个人的自荐信
2013/11/19 职场文书
学校校庆演讲稿
2014/05/22 职场文书
弘扬焦裕禄精神走群众路线思想汇报
2014/09/12 职场文书
离婚协议书应该怎么写
2014/10/12 职场文书
2014群众路线学习笔记
2014/11/06 职场文书
会计出纳岗位职责
2015/03/31 职场文书
《曾国藩家书》读后感——读家书,立家风
2019/08/21 职场文书
Python可视化神器pyecharts绘制地理图表
2022/07/07 Python