基于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 相关文章推荐
XML的代替者----JSON
Jul 21 Javascript
JQuery Tips(2) 关于$()包装集你不知道的
Dec 14 Javascript
AJAX使用了UpdatePanel后无法使用alert弹出脚本
Apr 02 Javascript
js下获得客户端操作系统的函数代码(1:vista,2:windows7,3:2000,4:xp,5:2003,6:2008)
Oct 31 Javascript
java、javascript实现附件下载示例
Aug 14 Javascript
JavaScript删除指定子元素代码实例
Jan 13 Javascript
jQuery中大家不太了解的几个方法
Mar 04 Javascript
jQuery animate和CSS3相结合实现缓动追逐效果附源码下载
Apr 18 Javascript
JavaScript编程设计模式之构造器模式实例分析
Oct 25 Javascript
Vue中的v-for指令不起效果的解决方法
Sep 27 Javascript
深入理解JavaScript的值传递和引用传递
Oct 24 Javascript
Vue 2.0 中依赖注入 provide/inject组合实战
Jun 20 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
BBS(php &amp; mysql)完整版(三)
2006/10/09 PHP
PHP获取数组最后一个值的2种方法
2015/01/21 PHP
使用 PHPStorm 开发 Laravel
2015/03/24 PHP
Yii数据库缓存实例分析
2016/03/29 PHP
Laravel框架运行出错提示RuntimeException No application encryption key has been specified.解决方法
2019/04/02 PHP
laravel框架模型中非静态方法也能静态调用的原理分析
2019/11/23 PHP
js showModalDialog参数的使用详解
2014/01/07 Javascript
AngularJS中使用HTML5手机摄像头拍照
2016/02/22 Javascript
jQuery编写设置和获取颜色的插件
2017/01/09 Javascript
基于vue.js路由参数的实例讲解——简单易懂
2017/09/07 Javascript
微信小程序wepy框架笔记小结
2018/08/08 Javascript
React降级配置及Ant Design配置详解
2018/12/27 Javascript
浅谈小程序 setData学问多
2019/02/20 Javascript
Vue Router 实现动态路由和常见问题及解决方法
2020/03/06 Javascript
vue监听浏览器原生返回按钮,进行路由转跳操作
2020/09/09 Javascript
[02:09:59]火猫TV国士无双dota2 6.82版本详解(下)
2014/09/29 DOTA
[56:17]NB vs Infamous 2019国际邀请赛淘汰赛 败者组 BO3 第三场 8.22
2019/09/05 DOTA
在Python的setuptools框架下生成egg的教程
2015/04/13 Python
浅谈Python中copy()方法的使用
2015/05/21 Python
Python Queue模块详细介绍及实例
2016/12/27 Python
Python中shape计算矩阵的方法示例
2017/04/21 Python
python中MethodType方法介绍与使用示例
2017/08/03 Python
简单实现python画圆功能
2018/01/25 Python
使用apidoc管理RESTful风格Flask项目接口文档方法
2018/02/07 Python
python导入模块交叉引用的方法
2019/01/19 Python
Python获取网段内ping通IP的方法
2019/01/31 Python
python 实现逻辑回归
2020/12/30 Python
IBatis持久层技术
2016/07/18 面试题
仓库班组长岗位职责
2013/12/12 职场文书
纪念建党演讲稿范文
2014/01/13 职场文书
应届生找工作求职信
2014/06/24 职场文书
公司领导九九重阳节发言稿2014
2014/09/25 职场文书
水电工岗位职责
2015/02/14 职场文书
2015年组织部工作总结
2015/04/03 职场文书
再读《皇帝的新衣》的读后感悟!
2019/08/07 职场文书
python数据可视化使用pyfinance分析证券收益示例详解
2021/11/20 Python