基于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 相关文章推荐
JavaScript 指导方针
Apr 05 Javascript
6个DIV 135或246间隔一秒轮番显示效果
Jul 24 Javascript
JS检测图片大小的实例
Aug 21 Javascript
使用POST方式弹出窗口的两种方法示例介绍
Jan 29 Javascript
jquery插件hiAlert实现网页对话框美化
May 03 Javascript
jQuery+Pdo编写login登陆界面
Aug 01 Javascript
详解如何使用Node.js编写命令工具——以vue-cli为例
Jun 29 Javascript
详解前端路由实现与react-router使用姿势
Aug 07 Javascript
jQuery的Ajax接收java返回数据方法
Aug 11 jQuery
Vue异步组件处理路由组件加载状态的解决方案
Sep 07 Javascript
webpack 如何同时输出压缩和未压缩的文件的实现步骤
Jun 05 Javascript
11个Javascript小技巧帮你提升代码质量(小结)
Dec 28 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
《猛禽小队》:DC宇宙的又一超级大烂片
2020/04/09 欧美动漫
利用discuz实现PHP大文件上传应用实例代码
2008/11/14 PHP
PHP 强制下载文件代码
2010/10/24 PHP
学习php设计模式 php实现合成模式(composite)
2015/12/08 PHP
PHP中的Trait 特性及作用
2016/04/03 PHP
纯CSS3实现质感细腻丝滑按钮
2021/03/09 HTML / CSS
jquery 双色表格实现代码
2009/12/08 Javascript
javascript简单事件处理和with用法介绍
2013/09/16 Javascript
jQuery 复合选择器应用的几个例子
2014/09/11 Javascript
文字垂直滚动之javascript代码
2015/07/29 Javascript
JS弹出对话框实现方法(三种方式)
2015/12/18 Javascript
JS在一定时间内跳转页面及各种刷新页面的实现方法
2016/05/26 Javascript
js基础之DOM中document对象的常用属性方法详解
2016/10/28 Javascript
如何实现iframe父子传参通信
2020/02/05 Javascript
[02:37]TI8勇士令状不朽珍藏II视频展示
2018/06/23 DOTA
[01:15]PWL S2开团时刻第二期——他们杀 我就白给
2020/11/25 DOTA
python实现kNN算法
2017/12/20 Python
python email smtplib模块发送邮件代码实例
2018/04/26 Python
django 通过url实现简单的权限控制的例子
2019/08/16 Python
python3实现的zip格式压缩文件夹操作示例
2019/08/17 Python
Python 多线程搜索txt文件的内容,并写入搜到的内容(Lock)方法
2019/08/23 Python
Python彻底删除文件夹及其子文件方式
2019/12/23 Python
keras 权重保存和权重载入方式
2020/05/21 Python
基于Keras的格式化输出Loss实现方式
2020/06/17 Python
html5 canvas实现跟随鼠标旋转的箭头
2016/03/11 HTML / CSS
HTML5 Video/Audio播放本地文件示例介绍
2013/11/18 HTML / CSS
英国文胸专家:AmpleBosom.com
2018/02/06 全球购物
大学生求职简历的自我评价
2013/10/14 职场文书
毕业生造价工程师求职信
2013/10/17 职场文书
项目计划书范文
2014/01/09 职场文书
大型车展策划方案
2014/02/01 职场文书
幼儿园小班班务总结
2015/08/03 职场文书
寒假生活随笔
2015/08/15 职场文书
如何利用pygame实现打飞机小游戏
2021/05/30 Python
CSS中Single Div 绘图技巧的实现
2021/06/18 HTML / CSS
Django对接elasticsearch实现全文检索的示例代码
2021/08/02 Python