基于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更改class和id的方法
Oct 10 Javascript
jQuery .attr()和.removeAttr()方法操作元素属性示例
Jul 16 Javascript
jQuery实现复选框全选/取消全选/反选及获得选择的值
Jun 12 Javascript
Jquery实现鼠标移动放大图片功能实例
Mar 25 Javascript
Jquery实现$.fn.extend和$.extend函数
Apr 14 Javascript
JavaScript中setTimeout的那些事儿
Nov 14 Javascript
angular1配合gulp和bower的使用教程
Jan 19 Javascript
ES6 迭代器(Iterator)和 for.of循环使用方法学习(总结)
Feb 08 Javascript
layui 数据表格 根据值(1=业务,2=机构)显示中文名称示例
Oct 26 Javascript
微信小程序个人中心的列表控件实现代码
Apr 26 Javascript
为什么JavaScript中0.1 + 0.2 != 0.3
Dec 03 Javascript
Webpack3+React16代码分割的实现
Mar 03 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提取字符串中的图片地址[正则表达式]
2011/11/12 PHP
百万级别知乎用户数据抓取与分析之PHP开发
2015/09/28 PHP
PHP读取并输出XML文件数据的简单实现方法
2017/12/22 PHP
从javascript语言本身谈项目实战
2006/12/27 Javascript
YUI的Tab切换实现代码
2010/04/11 Javascript
跟我学Node.js(四)---Node.js的模块载入方式与机制
2014/06/04 Javascript
JavaScript中指定函数名称的相关方法
2015/06/04 Javascript
JavaScript如何动态创建table表格
2020/08/02 Javascript
JavaScript常用数组算法小结
2016/02/13 Javascript
浅谈bootstrap源码分析之scrollspy(滚动侦听)
2016/06/06 Javascript
AngularJs定制样式插入到ueditor中的问题小结
2016/08/01 Javascript
AngularJS 自定义过滤器详解及实例代码
2016/09/14 Javascript
第一次接触神奇的Bootstrap
2016/10/14 Javascript
关于foreach循环中遇到的问题小结
2017/05/08 Javascript
浅谈NodeJs之数据库异常处理
2017/10/25 NodeJs
基于Three.js实现360度全景图片
2018/12/30 Javascript
layui点击左侧导航栏,实现不刷新整个页面,只刷新局部的方法
2019/09/25 Javascript
vue element-ui读取pdf文件的方法
2019/11/26 Javascript
在vue中实现echarts随窗体变化
2020/07/27 Javascript
vue组件中节流函数的失效的原因和解决方法
2020/12/02 Vue.js
Python装饰器使用示例及实际应用例子
2015/03/06 Python
使用Python神器对付12306变态验证码
2016/01/05 Python
Python图形绘制操作之正弦曲线实现方法分析
2017/12/25 Python
Python面向对象程序设计之私有变量,私有方法原理与用法分析
2020/03/23 Python
python安装读取grib库总结(推荐)
2020/06/24 Python
pycharm远程连接服务器并配置python interpreter的方法
2020/12/23 Python
HTML 5 标签、属性、事件及浏览器兼容性速查表 附打包下载
2012/10/20 HTML / CSS
浅谈h5自定义audio(问题及解决)
2016/08/19 HTML / CSS
敏捷开发的主要原则都有哪些
2015/04/26 面试题
保险专业大专生求职信
2013/10/26 职场文书
事业单位竞聘上岗实施方案
2014/03/28 职场文书
普通党员个人整改措施
2014/10/27 职场文书
计划生育责任书
2015/05/09 职场文书
经费申请报告范文
2015/05/18 职场文书
python利用while求100内的整数和方式
2021/11/07 Python
动态规划之使用备忘录来改进Javascript函数
2022/04/07 Javascript