基于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 相关文章推荐
在chrome浏览器中,防止input[text]和textarea在聚焦时出现黄色边框的解决方法
May 24 Javascript
文本框根据输入内容自适应高度的代码
Oct 24 Javascript
JavaScript获得当前网页来源页面(即上一页)的方法
Apr 03 Javascript
node.js从数据库获取数据
May 08 Javascript
浅析JavaScript函数的调用模式
Aug 10 Javascript
浅析vue component 组件使用
Mar 06 Javascript
Vue中引入样式文件的方法
Aug 18 Javascript
微信小程序实现图片压缩功能
Jan 26 Javascript
实例学习JavaScript读取和写入cookie
Jan 29 Javascript
小程序双头slider选择器的实现示例
Mar 31 Javascript
vue实现禁止浏览器记住密码功能的示例代码
Feb 03 Vue.js
JavaScript架构搭建前端监控如何采集异常数据
Jun 25 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
ThinkPHP与PHPExcel冲突解决方法
2011/08/08 PHP
Laravel关联模型中过滤结果为空的结果集(has和with区别)
2018/10/18 PHP
JQuery记住用户名和密码的具体实现
2014/04/04 Javascript
JS中使用Array函数shift和pop创建可忽略参数的例子
2014/05/28 Javascript
深入浅析JavaScript中对事件的三种监听方式
2015/09/29 Javascript
原生JS封装Ajax插件(同域、jsonp跨域)
2016/05/03 Javascript
AngularJS实现单独作用域内的数据操作
2016/09/05 Javascript
jQuery.cookie.js实现记录最近浏览过的商品功能示例
2017/01/23 Javascript
JS获取子、父、兄节点方法小结
2017/08/14 Javascript
浅谈webpack编译vue项目生成的代码探索
2017/12/11 Javascript
webpack-dev-server远程访问配置方法
2018/02/22 Javascript
vue头部导航动态点击处理方法
2018/11/02 Javascript
详解Vue demo实现商品列表的展示
2019/05/07 Javascript
如何在VUE中使用vue-awesome-swiper
2021/01/04 Vue.js
[05:08]顺网杯ISS-DOTA2赛歌 少女偶像Lunar青春演绎
2013/12/05 DOTA
Python标准库内置函数complex介绍
2014/11/25 Python
在Python的gevent框架下执行异步的Solr查询的教程
2015/04/16 Python
Python判断某个用户对某个文件的权限
2016/10/13 Python
python中的turtle库函数简单使用教程
2018/07/23 Python
django session完成状态保持的方法
2018/11/27 Python
python接口自动化(十六)--参数关联接口后传(详解)
2019/04/16 Python
python实现图片插入文字
2019/11/26 Python
Django Form and ModelForm的区别与使用
2019/12/06 Python
Python爬虫使用bs4方法实现数据解析
2020/08/25 Python
Python常用GUI框架原理解析汇总
2020/12/07 Python
50个强大璀璨的CSS3/JS技术运用实例
2010/02/27 HTML / CSS
元旦晚会策划方案
2014/02/18 职场文书
读书活动总结范文
2014/04/26 职场文书
红色故事演讲稿
2014/05/22 职场文书
完整版商业计划书
2014/09/15 职场文书
家属慰问信
2015/02/14 职场文书
小学庆六一主持词
2015/06/30 职场文书
加薪申请书应该这样写!
2019/07/04 职场文书
Python实现视频自动打码的示例代码
2022/04/08 Python
Golang MatrixOne使用介绍和汇编语法
2022/04/19 Golang
Python采集壁纸并实现炫轮播
2022/04/30 Python