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 相关文章推荐
jquery last-child 列表最后一项的样式
Jan 22 Javascript
jQuery 表单验证扩展(三)
Oct 20 Javascript
jQuery调取jSon数据并展示的方法
Jan 29 Javascript
使用struts2+Ajax+jquery验证用户名是否已被注册
Mar 22 Javascript
jQuery使用$.each遍历json数组的简单实现方法
Apr 18 Javascript
DIV+CSS+jQ实现省市联动可扩展
Jun 22 Javascript
Jquery Easyui菜单组件Menu使用详解(15)
Dec 18 Javascript
Javascript中构造函数要注意的一些坑
Jan 23 Javascript
微信小程序 chooseImage选择图片或者拍照
Apr 07 Javascript
把JavaScript代码改成ES6语法不完全指南(分享)
Sep 10 Javascript
js数据类型检测总结
Aug 05 Javascript
PHP 502bad gateway原因及解决方案
Nov 13 Javascript
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
解析php中两种缩放图片的函数,为图片添加水印
2013/06/14 PHP
浅谈COOKIE和SESSION区别
2015/07/19 PHP
Javascript实例教程(19) 使用HoTMetal(1)
2006/12/23 Javascript
用javascript实现给图片加链接
2007/08/15 Javascript
JS解密入门 最终变量劫持
2008/06/25 Javascript
javascript dom 基本操作小结
2010/04/11 Javascript
jquery实现图片按比例缩放示例
2014/07/01 Javascript
jQuery的图片滑块焦点图插件整理推荐
2014/12/07 Javascript
Javascript中获取对象的原型对象的方法小结
2015/02/25 Javascript
JS基于面向对象实现的放烟花效果
2015/05/07 Javascript
javascript框架设计之种子模块
2015/06/23 Javascript
JS+CSS实现仿雅虎另类滑动门切换效果
2015/10/13 Javascript
jQuery增加与删除table列的方法
2016/03/01 Javascript
Node.js测试中的Mock文件系统详解
2016/11/21 Javascript
Vue-Access-Control 前端用户权限控制解决方案
2017/12/01 Javascript
基于vue实现网站前台的权限管理(前后端分离实践)
2018/01/13 Javascript
解决在vue项目中,发版之后,背景图片报错,路径不对的问题
2018/03/06 Javascript
JS实现移动端触屏拖拽功能
2018/07/31 Javascript
解决vue 界面在苹果手机上滑动点击事件等卡顿问题
2018/11/27 Javascript
vue实现滑动超出指定距离回顶部功能
2019/07/31 Javascript
微信小程序实现左侧滑栏过程解析
2019/08/26 Javascript
Vue-CLI 3 scp2自动部署项目至服务器的方法
2020/07/24 Javascript
python psutil库安装教程
2018/03/19 Python
Python+pyplot绘制带文本标注的柱状图方法
2019/07/08 Python
超全Python图像处理讲解(多模块实现)
2020/04/13 Python
CSS3色彩模式有哪些?CSS3 HSL色彩模式的定义
2016/04/26 HTML / CSS
canvas之万花筒效果的简单实现(推荐)
2016/08/16 HTML / CSS
英国领先的高级美容和在线皮肤诊所:Face the Future
2020/06/17 全球购物
医学院护理专业应届生求职信
2013/11/12 职场文书
巧克力蛋糕店创业计划书
2014/01/14 职场文书
党的群众路线教育实践活动动员会主持词
2014/03/20 职场文书
蛋糕店创业计划书范文
2014/09/21 职场文书
授权委托书协议书
2014/10/16 职场文书
《最后一头战象》读后感:动物也有感情
2020/01/02 职场文书
Windows11里微软已经将驱动程序安装位置A盘删除
2021/11/21 数码科技
TV动画《八十龟酱观察日记》第四季宣传PV公布
2022/04/06 日漫