兼容IE和FF的图片上传前预览js代码


Posted in Javascript onMay 28, 2013

效果图如下:
兼容IE和FF的图片上传前预览js代码 
代码如下:

<!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>本地图片预览</title> 
<style type="text/css"> 
#preview{width:100px;height:100px;border:1px solid #000;overflow:hidden;} 
#imghead {filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);} 
</style> 
<script type="text/javascript"> 
function previewImage(file) 
{ 
var MAXWIDTH = 100; 
var MAXHEIGHT = 100; 
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 
{ 
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;margin-left:"+rect.left+"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='../images/head01_big.jpg'> 
</div> 
<br/> 
<input type="file" onchange="previewImage(this)" /> 
</body> 
</html>
Javascript 相关文章推荐
仿淘宝TAB切换搜索框搜索切换的相关内容
Sep 21 Javascript
Jquery 实现弹出层插件
Jan 28 Javascript
jQuery+CSS3折叠卡片式下拉列表框实现效果
Nov 02 Javascript
Knockoutjs 学习系列(二)花式捆绑
Jun 07 Javascript
JavaScript生成带有缩进的表格代码
Jun 15 Javascript
vue中页面跳转拦截器的实现方法
Aug 23 Javascript
Vue中render方法的使用详解
Jan 26 Javascript
解决vue 打包发布去#和页面空白的问题
Sep 04 Javascript
Node.js npm命令运行node.js脚本的方法
Oct 10 Javascript
详解angularjs跨页面传参遇到的一些问题
Nov 01 Javascript
ES6基础之 Promise 对象用法实例详解
Aug 22 Javascript
浅谈Vuex的this.$store.commit和在Vue项目中引用公共方法
Jul 24 Javascript
Extjs中ComboBoxTree实现的下拉框树效果(自写)
May 28 #Javascript
jQuery实现可拖动的浮动层完整代码
May 27 #Javascript
Jquery实现视频播放页面的关灯开灯效果
May 27 #Javascript
jquery scrollTop方法根据滚动像素显示隐藏顶部导航条
May 27 #Javascript
jquery实现的可隐藏重现的靠边悬浮层实例代码
May 27 #Javascript
jQuery解决下拉框select设宽度时IE 6/7/8下option超出显示不全
May 27 #Javascript
div当滚动到页面顶部的时候固定在顶部实例代码
May 27 #Javascript
You might like
实现php加速的eAccelerator dll支持文件打包下载
2007/09/30 PHP
php 图片加水印与上传图片加水印php类
2010/05/12 PHP
PHP实现通过正则表达式替换回调的内容标签
2015/06/15 PHP
thinkPHP5 ACL用户权限模块用法详解
2017/05/10 PHP
Javascript的一种模块模式
2010/09/08 Javascript
jQuery示例收集
2010/11/05 Javascript
借助script进行Http跨域请求:JSONP实现原理及代码
2013/03/19 Javascript
关于js注册事件的常用方法
2013/04/03 Javascript
js window.print实现打印特定控件或内容
2013/09/16 Javascript
JavaScript中的this关键字使用方法总结
2015/03/13 Javascript
js实现分享到随页面滚动而滑动效果的方法
2015/04/10 Javascript
bootstrap导航栏、下拉菜单、表单的简单应用实例解析
2017/01/06 Javascript
js中toString()和String()区别详解
2017/03/23 Javascript
Vue 项目部署到服务器的问题解决方法
2017/12/05 Javascript
详解vue2.0 资源文件assets和static的区别
2018/11/27 Javascript
详解vue挂载到dom上会发生什么
2019/01/20 Javascript
element form 校验数组每一项实例代码
2019/10/10 Javascript
Vue 技巧之控制父类的 slot
2020/02/24 Javascript
在vue中使用el-tab-pane v-show/v-if无效的解决
2020/08/03 Javascript
[52:52]完美世界DOTA2联赛PWL S3 LBZS vs access 第一场 12.10
2020/12/13 DOTA
Python的Django应用程序解决AJAX跨域访问问题的方法
2016/05/31 Python
详解Python中的type和object
2018/08/15 Python
python实现电子产品商店
2019/02/26 Python
PyTorch搭建一维线性回归模型(二)
2019/05/22 Python
python调用webservice接口的实现
2019/07/12 Python
对Python中一维向量和一维向量转置相乘的方法详解
2019/08/26 Python
python查看数据类型的方法
2019/10/12 Python
解决IDEA 的 plugins 搜不到任何的插件问题
2020/05/04 Python
英国最大的经认证的有机超市:Planet Organic
2018/02/02 全球购物
统计学教授推荐信
2014/09/18 职场文书
四风问题对照检查材料整改措施
2014/09/27 职场文书
渠道运营商合作协议书范本
2014/10/06 职场文书
班干部学习委员竞选稿
2015/11/20 职场文书
2016计算机专业毕业生自荐信
2016/01/28 职场文书
十大最强飞行系宝可梦,BUG燕上榜,第二是飞行系王者
2022/03/18 日漫
Python&Matlab实现樱花的绘制
2022/04/07 Python