基于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 相关文章推荐
用 JSON 处理缓存
Apr 27 Javascript
node.js中的fs.lchownSync方法使用说明
Dec 16 Javascript
node.js中的fs.mkdirSync方法使用说明
Dec 17 Javascript
DIV+CSS+jQ实现省市联动可扩展
Jun 22 Javascript
jQuery post数据至ashx实例详解
Nov 18 Javascript
jQuery实现加入收藏夹功能(主流浏览器兼职)
Dec 24 Javascript
js图片延迟加载(Lazyload)三种实现方式
Mar 01 Javascript
浅谈Vue父子组件和非父子组件传值问题
Aug 22 Javascript
JS实现按钮颜色切换效果
Sep 05 Javascript
小程序使用watch监听数据变化的方法详解
Sep 20 Javascript
jquery中attr、prop、data区别与用法分析
Sep 25 jQuery
JavaScript实现雪花飘落效果
Dec 27 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中动态HTML的输出技术
2006/10/09 PHP
PHP中::、-&amp;gt;、self、$this几种操作符的区别介绍
2013/04/24 PHP
php正则匹配文章中的远程图片地址并下载图片至本地
2015/09/29 PHP
浅析PHP中call user func()函数及如何使用call user func调用自定义函数
2015/11/05 PHP
使用PHP实现微信摇一摇周边红包
2016/01/04 PHP
PHP中的密码加密的解决方案总结
2016/10/26 PHP
php微信公众号js-sdk开发应用
2016/11/28 PHP
PHP/HTML混写的四种方式总结
2017/02/27 PHP
Thinkphp事务操作实例(推荐)
2017/04/01 PHP
php微信开发之关键词回复功能
2018/06/13 PHP
PHP实现的62进制转10进制,10进制转62进制函数示例
2019/06/06 PHP
thinkphp框架使用JWTtoken的方法详解
2019/10/10 PHP
jQuery实现可拖动的浮动层完整代码
2013/05/27 Javascript
Jquery.addClass始终无效原因分析
2013/09/08 Javascript
js获取浏览器基本信息大全
2014/11/27 Javascript
jQuery实现的省市联动菜单功能示例【测试可用】
2017/01/13 Javascript
JS控制鼠标拒绝点击某一按钮的实例
2017/12/29 Javascript
JS动态插入脚本和插入引用外部链接脚本的方法
2018/05/21 Javascript
JavaScript 判断对象中是否有某属性的常用方法
2018/06/14 Javascript
Bootstrap table表格初始化表格数据的方法
2018/07/25 Javascript
Nodejs实现的操作MongoDB数据库功能完整示例
2019/02/02 NodeJs
vue.js实现图书管理功能
2019/09/24 Javascript
vue中的mescroll搜索运用及各种填坑处理
2019/10/30 Javascript
Vue实现点击当前行变色
2020/12/14 Vue.js
Pycharm 设置默认头的图文教程
2019/01/17 Python
Python随机函数库random的使用方法详解
2019/08/21 Python
python字符串替换re.sub()方法解析
2019/09/18 Python
python 读取二进制 显示图片案例
2020/04/24 Python
python的json包位置及用法总结
2020/06/21 Python
HTML5新增属性data-*和js/jquery之间的交互及注意事项
2017/08/08 HTML / CSS
html5给汉字加拼音加进度条的实现代码
2020/04/07 HTML / CSS
StubHub墨西哥:购买和出售您的门票
2016/09/17 全球购物
缴纳养老保险的证明
2014/01/10 职场文书
大学共青团员个人自我评价
2014/04/16 职场文书
婚礼迎宾词大全
2015/08/10 职场文书
新手,如何业余时间安排好写作、提高写作能力?
2019/10/21 职场文书