基于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获得地址栏参数的两种方法
Nov 08 Javascript
javascript动态添加表格数据行(ASP后台数据库保存例子)
May 08 Javascript
jQuery 回车事件enter使用示例
Feb 18 Javascript
jQuery中ajax的get()方法用法实例
Dec 26 Javascript
Javascript基础教程之数据类型转换
Jan 18 Javascript
jQuery使用hide方法隐藏指定元素class样式用法实例
Mar 30 Javascript
javascript实现动态改变层大小的方法
May 14 Javascript
jQuery实现链接的title快速出现的方法
Feb 20 Javascript
vue中的 $slot 获取插槽的节点实例
Nov 12 Javascript
vue父子模板传值问题解决方法案例分析
Feb 26 Javascript
JS实现鼠标按下拖拽效果
Jul 23 Javascript
Vue获取微博授权URL代码实例
Nov 04 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管理内存函数 memory_get_usage()使用介绍
2012/09/23 PHP
php gzip压缩输出的实现方法
2013/04/27 PHP
php基于websocket搭建简易聊天室实践
2016/10/24 PHP
自定义Laravel (monolog)日志位置,并增加请求ID的实现
2019/10/17 PHP
IE6浏览器下resize事件被执行了多次解决方法
2012/12/11 Javascript
jquery选择符快速提取web表单数据示例
2014/03/27 Javascript
javascript实现简单的分页特效
2015/08/12 Javascript
JS实现登录页面记住密码和enter键登录方法推荐
2016/05/10 Javascript
Markdown+Bootstrap图片自适应属性详解
2016/05/21 Javascript
Angular2 环境配置详细介绍
2016/09/21 Javascript
BootstrapTable refresh 方法使用实例简单介绍
2017/02/20 Javascript
EasyUI为Numberbox添加blur事件的方法
2017/03/05 Javascript
Angularjs 双向绑定时字符串的转换成数字类型的问题
2017/06/12 Javascript
nodejs搭建本地服务器轻松解决跨域问题
2018/03/21 NodeJs
详解通过源码解析Node.js中cluster模块的主要功能实现
2018/05/16 Javascript
vue项目中jsonp跨域获取qq音乐首页推荐问题
2018/05/30 Javascript
模块化react-router配置方法详解
2019/06/03 Javascript
python BeautifulSoup使用方法详解
2013/11/21 Python
在Python中处理XML的教程
2015/04/29 Python
Python中if __name__ == '__main__'作用解析
2015/06/29 Python
python各种语言间时间的转化实现代码
2016/03/23 Python
Python设计模式之策略模式实例详解
2019/01/21 Python
Python面向对象程序设计示例小结
2019/01/30 Python
python如何制作缩略图
2019/04/30 Python
Python开发之Nginx+uWSGI+virtualenv多项目部署教程
2019/05/13 Python
PYTHON绘制雷达图代码实例
2019/10/15 Python
python中图像通道分离与合并实例
2020/01/17 Python
django model的update时auto_now不被更新的原因及解决方式
2020/04/01 Python
英国女性化妆品收纳和家具网站:Beautify
2019/12/07 全球购物
Java语言程序设计测试题选择题部分
2014/04/03 面试题
《中彩那天》教学反思
2014/02/22 职场文书
商铺门前三包责任书
2014/07/25 职场文书
副校长竞聘演讲稿
2014/09/01 职场文书
优秀班集体事迹材料
2014/12/25 职场文书
2016三严三实专题教育活动心得体会
2016/01/06 职场文书
golang中的struct操作
2021/11/11 Golang