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 Event学习第六章 事件的访问
Feb 07 Javascript
解决jquery的datepicker的本地化以及Today问题
May 23 Javascript
jQuery学习笔记 操作jQuery对象 CSS处理
Sep 19 Javascript
javascript时间函数基础介绍
Mar 28 Javascript
js改变文章字体大小的实例代码
Nov 27 Javascript
jq实现酷炫的鼠标经过图片翻滚效果
Mar 12 Javascript
JavaScript中获取Radio被选中的值
Nov 11 Javascript
ArtEditor富文本编辑器增加表单提交功能
Apr 18 Javascript
基于Vue实例生命周期(全面解析)
Aug 16 Javascript
Vue组件教程之Toast(Vue.extend 方式)详解
Jan 27 Javascript
jQuery 常用特效实例小结【显示与隐藏、淡入淡出、滑动、动画等】
May 19 jQuery
基于JavaScript实现大文件上传后端代码实例
Aug 18 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 输出双引号&quot;与单引号'的方法
2010/05/09 PHP
提高PHP性能的编码技巧以及性能优化详细解析
2013/08/24 PHP
php无限级分类实现方法分析
2016/10/19 PHP
PHP设计模式之委托模式定义与用法简单示例
2018/08/13 PHP
DOM相关内容速查手册
2007/02/07 Javascript
JavaScript中window、doucment、body的解释
2013/08/14 Javascript
Javascript判断文件是否存在(客户端/服务器端)
2014/09/16 Javascript
js中javascript:void(0) 真正含义
2020/11/05 Javascript
纯javascript代码实现计算器功能(三种方法)
2015/09/07 Javascript
JavaScript 监控微信浏览器且自带返回按钮时间
2016/11/27 Javascript
JS实现瀑布流布局
2017/10/21 Javascript
利用babel将es6语法转es5的简单示例
2017/12/01 Javascript
react+redux仿微信聊天界面
2019/06/21 Javascript
jQuery实现中奖播报功能(让文本滚动起来) 简单设置数值即可
2020/03/20 jQuery
js实现浏览器打印功能的示例代码
2020/07/15 Javascript
如何在Vue.JS中使用图标组件
2020/08/04 Javascript
前端开发基础javaScript的六大作用
2020/08/06 Javascript
Vue+Element自定义纵向表格表头教程
2020/10/26 Javascript
微信小程序input抖动问题的修复方法
2021/03/03 Javascript
[38:41]2014 DOTA2国际邀请赛中国区预选赛 LGD VS CNB
2014/05/22 DOTA
[03:04]2018年国际邀请赛典藏宝瓶&莱恩声望物品展示 片尾有彩蛋
2018/06/04 DOTA
精确查找PHP WEBSHELL木马的方法(1)
2011/04/12 Python
举例介绍Python中的25个隐藏特性
2015/03/30 Python
利用Python库Scapy解析pcap文件的方法
2019/07/23 Python
Python 实现将某一列设置为str类型
2020/07/14 Python
python中字典增加和删除使用方法
2020/09/30 Python
基于python爬取梨视频实现过程解析
2020/11/09 Python
python中绕过反爬虫的方法总结
2020/11/25 Python
html5播放视频且动态截图实现步骤与代码(支持safari其他未测试)
2013/01/06 HTML / CSS
HTML5高仿微信聊天、微信聊天表情|对话框|编辑器功能
2018/04/23 HTML / CSS
澳大利亚票务和娱乐市场领导者:Ticketmaster
2017/03/03 全球购物
味多美官网:蛋糕订购,100%使用天然奶油
2017/11/10 全球购物
索尼巴西商店:Sony巴西
2019/06/21 全球购物
亚马逊巴西站:Amazon.com.br
2019/09/22 全球购物
自我检讨书范文
2015/01/28 职场文书
python缺失值填充方法示例代码
2022/12/24 Python