基于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 相关文章推荐
Extjs 继承Ext.data.Store不起作用原因分析及解决
Apr 15 Javascript
angularjs实现与服务器交互分享
Jun 24 Javascript
JavaScript判断表单为空及获取焦点的方法
Feb 12 Javascript
js实现图片360度旋转
Jan 22 Javascript
node操作mysql数据库实例详解
Mar 17 Javascript
HTML的select控件美化
Mar 27 Javascript
JavaScript变量作用域_动力节点Java学院整理
Jun 27 Javascript
JavaScript学习总结之正则的元字符和一些简单的应用
Jun 30 Javascript
JavaScript实现的斑马线表格效果【隔行变色】
Sep 18 Javascript
js点击时关闭该范围下拉菜单之外的菜单方法
Jan 11 Javascript
node 版本切换的实现
Feb 02 Javascript
vue项目使用高德地图的定位及关键字搜索功能的实例代码(踩坑经验)
Mar 07 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/05/07 PHP
PHP Echo字符串的连接格式
2016/03/07 PHP
Yii2选项卡的简单使用
2017/05/26 PHP
jquery 层次选择器siblings与nextAll的区别介绍
2013/08/02 Javascript
js中的数组Array定义与sort方法使用示例
2013/08/29 Javascript
当json键为数字时的取值方法解析
2013/11/15 Javascript
javascript学习笔记--数字格式类型
2014/05/22 Javascript
jquery实现文本框的禁用和启用
2016/12/07 Javascript
jQuery插件HighCharts绘制2D饼图效果示例【附demo源码下载】
2017/03/21 jQuery
JavaScript实现跟随滚动缓冲运动广告框
2017/07/15 Javascript
老生常谈ES6中的类
2017/07/31 Javascript
详解react-redux插件入门
2018/04/19 Javascript
vue.extend与vue.component的区别和联系
2018/09/19 Javascript
JS实现商品橱窗特效
2020/01/09 Javascript
HTML+JS实现“代码雨”效果源码(黑客帝国文字下落效果)
2020/03/17 Javascript
React实现全选功能
2020/08/25 Javascript
jquery实现异步文件上传ajaxfileupload.js
2020/10/23 jQuery
vue中axios封装使用的完整教程
2021/03/03 Vue.js
python正则表达式re模块详解
2014/06/25 Python
python自动化测试之setUp与tearDown实例
2014/09/28 Python
Python多进程分块读取超大文件的方法
2016/04/13 Python
读写json中文ASCII乱码问题的解决方法
2016/11/05 Python
Django后台获取前端post上传的文件方法
2018/05/28 Python
Python设计模式之职责链模式原理与用法实例分析
2019/01/11 Python
numpy.linalg.eig() 计算矩阵特征向量方式
2019/11/29 Python
Win10用vscode打开anaconda环境中的python出错问题的解决
2020/05/25 Python
python 实现图像快速替换某种颜色
2020/06/04 Python
Python Selenium破解滑块验证码最新版(GEETEST95%以上通过率)
2021/01/29 Python
PyCharm常用配置和常用插件(小结)
2021/02/06 Python
Python中的流程控制详解
2021/02/18 Python
2014年元旦感言
2014/03/06 职场文书
医德医魂心得体会
2014/09/11 职场文书
2015年端午节国旗下演讲稿
2015/03/19 职场文书
新年晚会开场白
2015/05/29 职场文书
幼儿园六一儿童节主持词
2015/06/30 职场文书
sql server 累计求和实现代码
2022/02/28 SQL Server