js上传图片预览的实现方法


Posted in Javascript onMay 09, 2017

本文实例为大家分享了js上传图片预览的方法,供大家参考,具体内容如下

<html >

<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>图片上传本地预览</title>  
<style type="text/css">
#preview{width:260px;height:190px;border:1px solid #000;overflow:hidden;}
#imghead {filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);}
</style>
<script type="text/javascript">


    //图片上传预览 IE是用了滤镜。
  function previewImage(file)
  {
   var MAXWIDTH = 260; 
   var MAXHEIGHT = 180;
   var div = document.getElementById('preview');
   if (file.files && file.files[0])
   {
    div.innerHTML ='<img id=imghead>';
    var img = document.getElementById('imghead');
    img.onload = function(){
    var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
    img.width = rect.width;
    img.height = rect.height;
//     img.style.marginLeft = rect.left+'px';
    img.style.marginTop = rect.top+'px';
    }
    var reader = new FileReader();
    reader.onload = function(evt){img.src = evt.target.result;}
    reader.readAsDataURL(file.files[0]);
   }
   else //兼容IE
   {
   var sFilter='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
   file.select();
   var src = document.selection.createRange().text;
   div.innerHTML = '<img id=imghead>';
   var img = document.getElementById('imghead');
   img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
   var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
   status =('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height);
   div.innerHTML = "<div id=divhead style='width:"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;"+sFilter+src+"\"'></div>";
   }
  }
  function clacImgZoomParam( maxWidth, maxHeight, width, height ){
   var param = {top:0, left:0, width:width, height:height};
   if( width>maxWidth || height>maxHeight )
   {
    rateWidth = width / maxWidth;
    rateHeight = height / maxHeight;

    if( rateWidth > rateHeight )
    {
     param.width = maxWidth;
     param.height = Math.round(height / rateWidth);
    }else
    {
     param.width = Math.round(width / rateHeight);
     param.height = maxHeight;
    }
   }

   param.left = Math.round((maxWidth - param.width) / 2);
   param.top = Math.round((maxHeight - param.height) / 2);
   return param;
  }
</script>  
</head>  
<body>
<div id="preview">
 <img id="imghead" width=100 height=100 border=0 src='<%=request.getContextPath()%>/images/defaul.jpg'>
</div>


 <input type="file" onchange="previewImage(this)" />  
</body>  
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
用正则xmlHttp实现的偷(转)
Jan 22 Javascript
JS动态显示表格上下frame的方法
Mar 31 Javascript
jQuery处理图片加载失败的常用方法
Jun 08 Javascript
javascript返回顶部的按钮实现方法
Jan 09 Javascript
Webpack中css-loader和less-loader的使用教程
Apr 27 Javascript
javaScript实现复选框全选反选事件详解
Nov 20 Javascript
Vue2.0 实现移动端图片上传功能
May 30 Javascript
node.js调用C++函数的方法示例
Sep 21 Javascript
在 Vue-CLI 中引入 simple-mock实现简易的 API Mock 接口数据模拟
Nov 28 Javascript
微信小程序中weui用法解析
Oct 21 Javascript
JS+CSS+HTML实现“代码雨”类似黑客帝国文字下落效果
Mar 17 Javascript
jQuery实现简单QQ聊天框
Aug 27 jQuery
react开发中如何使用require.ensure加载es6风格的组件
May 09 #Javascript
Vue2单一事件管理组件通信
May 09 #Javascript
react-router实现按需加载
May 09 #Javascript
BootStrap表单时间选择器详解
May 09 #Javascript
兼容浏览器的js事件绑定函数(详解)
May 09 #Javascript
JS触摸与手势事件详解
May 09 #Javascript
Angualrjs 表单验证的两种方式(失去焦点验证和点击提交验证)
May 09 #Javascript
You might like
SONY ICF-F10中波修复记
2021/03/02 无线电
防止MySQL注入或HTML表单滥用的PHP程序
2009/01/21 PHP
PHP文件打开、关闭、写入的判断与执行代码
2011/05/24 PHP
PHP 杂谈《重构-改善既有代码的设计》之五 简化函数调用
2012/05/07 PHP
PHP Include文件实例讲解
2019/02/15 PHP
PHP命名空间(namespace)原理与用法详解
2019/12/11 PHP
thinkphp框架表单数组实现图片批量上传功能示例
2020/04/04 PHP
基于thinkphp5框架实现微信小程序支付 退款 订单查询 退款查询操作
2020/08/17 PHP
JQuery 初体验(建议学习jquery)
2009/04/25 Javascript
jQuery Animation实现CSS3动画示例介绍
2013/08/14 Javascript
网页中可关闭的漂浮窗口实现可自行调节
2013/08/20 Javascript
浅析IE10兼容性问题(frameset的cols属性)
2014/01/03 Javascript
jQuery scroll事件实现监控滚动条分页示例
2014/04/04 Javascript
Asp.Net之JS生成分页条的方法
2016/11/23 Javascript
jQuery Masonry瀑布流插件使用方法详解
2017/01/18 Javascript
微信小程序新增的拖动组件movable-view使用教程
2017/05/20 Javascript
js计算两个日期间的天数月的实例代码
2018/09/20 Javascript
Vue开发环境中修改端口号的实现方法
2019/08/15 Javascript
jQuery HTML css()方法与css类实例详解
2020/05/20 jQuery
微信小程序自定义底部弹出框功能
2020/11/18 Javascript
Python 闭包的使用方法
2017/09/07 Python
scrapy-redis的安装部署步骤讲解
2019/02/27 Python
Python实现制度转换(货币,温度,长度)
2019/07/14 Python
PyQtGraph在pyqt中的应用及安装过程
2019/08/04 Python
澳大利亚女装精品店:Alannah Hill
2020/07/29 全球购物
Yahoo-PHP面试题3
2012/01/14 面试题
销售人员中英文自荐信
2013/09/22 职场文书
开工庆典邀请函范文
2014/01/16 职场文书
技校个人求职信范文
2014/01/25 职场文书
企业宣传方案
2014/03/04 职场文书
2014年基层党支部工作总结
2014/12/04 职场文书
教师调动申请报告
2015/05/18 职场文书
辩论赛新闻稿
2015/07/17 职场文书
坚持不是死撑,更重要的是心态
2019/08/19 职场文书
浅谈Python中的函数(def)及参数传递操作
2021/05/25 Python
Win10鼠标轨迹怎么开 Win10显示鼠标轨迹方法
2022/04/06 数码科技