基于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 相关文章推荐
jQuery Ajax 仿AjaxPro.Utility.RegisterTypeForAjax辅助方法
Sep 27 Javascript
Javascript遍历table中的元素示例代码
Jul 08 Javascript
JS禁止查看网页源代码的实现方法
Oct 12 Javascript
原生js实现日期计算器功能
Feb 17 Javascript
vue如何获取点击事件源的方法
Aug 10 Javascript
微信小程序使用swiper组件实现类3D轮播图
Aug 29 Javascript
vue-cli 3.x 配置Axios(proxyTable)跨域代理方法
Sep 19 Javascript
angular中两种表单的区别(响应式和模板驱动表单)
Dec 06 Javascript
JS实现的杨辉三角【帕斯卡三角形】算法示例
Feb 26 Javascript
Array.filter中如何正确使用Async
Nov 04 Javascript
原生jQuery实现只显示年份下拉框
Dec 24 jQuery
vue+element UI实现树形表格
Dec 29 Vue.js
整理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加速 eAccelerator配置和使用指南
2009/06/05 PHP
浅析PHP安装扩展mcrypt以及相关依赖项(PHP安装PECL扩展的方法)
2013/07/05 PHP
ThinkPHP3.1查询语言详解
2014/06/19 PHP
PHP中array_map与array_column之间的关系分析
2014/08/19 PHP
php防止网站被刷新的方法汇总
2014/12/01 PHP
php 数组元素快速去重
2017/05/05 PHP
JAVASCRIPT  THIS详解 面向对象
2009/03/25 Javascript
jquery实现漂浮在网页右侧的qq在线客服插件示例
2013/05/13 Javascript
js控制表单不能输入空格的小例子
2013/11/20 Javascript
nw.js实现类似微信的聊天软件
2015/03/16 Javascript
jQuery实现左右切换焦点图
2015/04/03 Javascript
KnockoutJS 3.X API 第四章之表单textInput、hasFocus、checked绑定
2016/10/11 Javascript
Angular2生命周期钩子函数的详细介绍
2017/07/10 Javascript
vue弹窗插件实战代码
2018/09/08 Javascript
从vue源码看props的用法
2019/01/09 Javascript
npm ci命令的基本使用方法
2020/09/20 Javascript
python计算方程式根的方法
2015/05/07 Python
opencv python 图像去噪的实现方法
2018/08/31 Python
Python爬虫之UserAgent的使用实例
2019/02/21 Python
关于python字符串方法分类详解
2019/08/20 Python
Python自动采集微信联系人的实现示例
2020/02/28 Python
详解pytorch tensor和ndarray转换相关总结
2020/09/03 Python
python smtplib发送多个email联系人的实现
2020/10/09 Python
python函数超时自动退出的实操方法
2020/12/28 Python
Html5新标签解释及用法
2012/02/17 HTML / CSS
html5指南-3.如何实现html元素拖拽功能
2013/01/07 HTML / CSS
Ray-Ban雷朋西班牙官网:全球领先的太阳眼镜品牌
2018/11/28 全球购物
网络工程师个人的自我评价范文
2013/10/01 职场文书
大三自我鉴定范文
2013/10/05 职场文书
出纳岗位职责模板
2013/11/27 职场文书
学徒工职责
2014/03/06 职场文书
会计学毕业生求职信
2014/06/25 职场文书
最美家庭活动方案
2014/08/31 职场文书
公司授权委托书范文
2014/09/21 职场文书
毕业生银行实习自我鉴定
2014/10/14 职场文书
跟班学习心得体会(共6篇)
2016/01/23 职场文书