基于javascript代码实现通过点击图片显示原图片


Posted in Javascript onNovember 29, 2015

废话不多说了,直接给大家贴js实现点击图片显示原图片的代码,具体代码如下所示:

function DrawImage(ImgD){
  var image = new Image();
  image.src=ImgD.src;
  var width = $(ImgD).attr("width");
  var height = $(ImgD).attr("height");
  if(width >100 && height>80){
    ImgD.width=100; 
    ImgD.height=80;
    ImgD.alt=image.width+"×"+image.height;
  }else{
    if(image.width>0 && image.height>0){
      flag=true;
      if(image.width>300 || image.height>200){
        ImgD.width=image.width/2; 
        ImgD.height=image.height/2;
        ImgD.alt=image.width+"×"+image.height;
      }else{
        ImgD.width=image.width;  
        ImgD.height=image.height;
        ImgD.alt=image.width+"×"+image.height;
      }
    }
  }
}

下面分享一段关于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: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 相关文章推荐
解析arp病毒背后利用的Javascript技术附解密方法
Aug 06 Javascript
基于jquery的DIV随滚动条滚动而滚动的代码
Jul 20 Javascript
jquery动态添加删除div 具体实现
Jul 20 Javascript
js和jquery如何获取图片真实的宽度和高度
Sep 28 Javascript
jquery+json实现动态商品内容展示的方法
Jan 14 Javascript
AngularJS中$interval的用法详解
Feb 02 Javascript
BootStrap框架中的data-[ ]自定义属性理解(推荐)
Feb 14 Javascript
详解基于vue的移动web app页面缓存解决方案
Aug 03 Javascript
bootstrap table支持高度百分比的实例代码
Feb 28 Javascript
node.js的Express服务器基本使用教程
Jan 09 Javascript
vue或react项目生产环境去掉console.log的操作
Sep 02 Javascript
cypress测试本地web应用
Jun 01 Javascript
整理Javascript数组学习笔记
Nov 29 #Javascript
Bootstrap每天必学之缩略图与警示窗
Nov 29 #Javascript
分享我的jquery实现下拉菜单心的
Nov 29 #Javascript
jQuery随手笔记之常用的jQuery操作DOM事件
Nov 29 #Javascript
整理Javascript基础语法学习笔记
Nov 29 #Javascript
Jquery操作Ajax方法小结
Nov 29 #Javascript
jquery+php实现滚动的数字特效
Nov 29 #Javascript
You might like
php解压文件代码实现php在线解压
2014/02/13 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(六)
2014/06/23 PHP
PHP处理CSV表格文件的常用操作方法总结
2016/07/01 PHP
php使用redis的有序集合zset实现延迟队列应用示例
2020/02/20 PHP
JavaScript的单例模式 (singleton in Javascript)
2010/06/11 Javascript
ExtJS4中使用mixins实现多继承示例
2013/12/03 Javascript
如何在node的express中使用socket.io
2014/12/15 Javascript
基于Bootstrap+jQuery.validate实现表单验证
2016/05/30 Javascript
bootstrap读书笔记之CSS组件(上)
2016/10/17 Javascript
详解vue + vuex + directives实现权限按钮的思路
2017/10/24 Javascript
js判断节假日实例代码
2017/12/27 Javascript
Vue组件通信的四种方式汇总
2018/02/08 Javascript
vue使用中的内存泄漏【推荐】
2018/07/10 Javascript
用Electron写个带界面的nodejs爬虫的实现方法
2019/01/29 NodeJs
微信小程序实现时间戳格式转换
2020/07/20 Javascript
Python实现列表删除重复元素的三种常用方法分析
2017/11/24 Python
python正则表达式面试题解答
2020/04/28 Python
Python实现的购物车功能示例
2018/02/11 Python
python获取时间及时间格式转换问题实例代码详解
2018/12/06 Python
WIn10+Anaconda环境下安装PyTorch(避坑指南)
2019/01/30 Python
python将pandas datarame保存为txt文件的实例
2019/02/12 Python
Python调用Redis的示例代码
2020/11/24 Python
英国最大的美妆产品在线零售商之一:Beauty Bay
2017/09/29 全球购物
Java里面有没有全局变量?为什么?
2015/02/06 面试题
EJB发布WEB服务一般步骤
2012/10/31 面试题
高级人员简历的自我评价分享
2013/11/03 职场文书
建筑工程管理专业自荐信范文
2013/12/28 职场文书
护士求职自荐信范文
2014/03/19 职场文书
给校长的建议书600字
2014/05/15 职场文书
教师师德考核自我评价
2014/09/13 职场文书
复试通知单模板
2015/04/24 职场文书
2015年售后服务工作总结
2015/04/25 职场文书
投资合作意向书范本
2015/05/08 职场文书
班级联欢会主持词
2015/07/03 职场文书
Python 实现绘制子图及子图刻度的变换等问题
2021/05/31 Python
ORACLE数据库应用开发的三十个注意事项
2021/06/07 Oracle