基于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 相关文章推荐
自适应高度框架 ----属个人收藏内容
Jan 22 Javascript
从sohu弄下来的flash中展示图片的代码
Apr 27 Javascript
Jquery UI震动效果实现原理及步骤
Feb 04 Javascript
jquery获取被勾选的checked(选中)的那一行的3列和4列的值
Jul 04 Javascript
html向js方法传递参数具体实现
Aug 08 Javascript
JS中的构造函数详细解析
Mar 10 Javascript
详解JavaScript基本类型和引用类型
Dec 09 Javascript
解析javascript瀑布流原理实现图片滚动加载
Mar 10 Javascript
浅谈js和css内联外联注意事项
Jun 30 Javascript
Vue 2.0在IE11中打开项目页面空白的问题解决
Jul 16 Javascript
浅谈Node异步编程的机制
Oct 18 Javascript
JavaScript类的继承方法小结【组合继承分析】
Jul 11 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
解析Win7 XAMPP apache无法启动的问题
2013/06/26 PHP
PHP5.3与5.5废弃与过期函数整理汇总
2014/07/10 PHP
微信公众平台开发之配置与请求
2015/08/26 PHP
Smarty模板变量与调节器实例详解
2019/07/20 PHP
RR vs IO BO3 第二场2.13
2021/03/10 DOTA
Jquery 获取表单text,areatext,radio,checkbox,select值的代码
2009/11/12 Javascript
js获取指定日期周数以及星期几的小例子
2014/06/27 Javascript
js查找节点的方法小结
2015/01/13 Javascript
JavaScript代码实现图片循环滚动效果
2020/03/19 Javascript
判断js的Array和Object的实现方法
2016/08/29 Javascript
JQuery和PHP结合实现动态进度条上传显示
2016/11/23 Javascript
Bootstrap基本样式学习笔记之图片(6)
2016/12/07 Javascript
jQuery插件FusionCharts实现的3D柱状图效果实例【附demo源码下载】
2017/03/03 Javascript
基于vue2框架的机器人自动回复mini-project实例代码
2017/06/13 Javascript
JavaScript的变量声明与声明提前用法实例分析
2019/11/26 Javascript
node.js开发辅助工具nodemon安装与配置详解
2020/02/06 Javascript
vue缓存之keep-alive的理解和应用详解
2020/11/02 Javascript
微信小程序实现购物车功能
2020/11/18 Javascript
详解Python中__str__和__repr__方法的区别
2015/04/17 Python
Python实现Youku视频批量下载功能
2017/03/14 Python
python利用Guetzli批量压缩图片
2017/03/23 Python
Python星号*与**用法分析
2018/02/02 Python
对Tensorflow中的变量初始化函数详解
2018/07/27 Python
解决python opencv无法显示图片的问题
2018/10/28 Python
pytorch点乘与叉乘示例讲解
2019/12/27 Python
Python Json数据文件操作原理解析
2020/05/09 Python
让Django的BooleanField支持字符串形式的输入方式
2020/05/20 Python
css3模拟jq点击事件的实例代码
2017/07/06 HTML / CSS
中国综合性网上购物商城:当当(网上卖书起家)
2016/11/16 全球购物
GetYourGuide台湾:预订旅游活动、景点和旅游项目
2019/06/10 全球购物
媒矿安全生产承诺书
2014/05/23 职场文书
优秀乡村医生事迹材料
2014/05/28 职场文书
最美乡村医生事迹材料
2014/06/02 职场文书
初中作文评语
2014/12/25 职场文书
党员个人承诺书
2015/04/27 职场文书
苹果电脑mac os中货币符号快捷输入
2022/02/17 杂记