罗伯斯基 2015-12-21 06:43 采纳率: 0%
浏览 1567

求助:在div块里上传图片时不能完整覆盖的问题

每次上传图片都不能完整的将图片覆盖,请求各路大神帮帮忙
css代码:

 .idBox input{width:8rem;height:2.2rem;-moz-appearance:none;margin-top:0.3rem;opacity:0;}
 .idBox img{width:3rem;position:absolute;top:0.8rem;}

html代码:

 <!DOCTYPE html>
<html lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<title>-title-</title>

<link rel="stylesheet" id="default-css" href="css/default.css" type="text/css" media="all">
<link rel="stylesheet" id="section-css" href="css/section.css" type="text/css" media="all">

<script type="text/javascript">

//图片上传预览    IE是用了滤镜。
  function previewImage(file,parameter)
  {

    var MAXWIDTH  = 270; 
    var MAXHEIGHT = 260;
      if(parameter==1){
    var div = document.getElementById('photo');}
      if(parameter==2){
    var div = document.getElementById('photo1');}
    if (file.files && file.files[0])
    {
          if(parameter==1){
        div.innerHTML ='<img id=imgs>';
        var img = document.getElementById('imgs');}
          if(parameter==2){
          div.innerHTML ='<img id=imgs1>';
        var img = document.getElementById('imgs1');
          }
        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=imgs>';
      var img = document.getElementById('imgs');
      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  style='width:"+rect.width+"px;height:"+rect.height+"px;;position:absolute;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 class="home"> 
    <div id="page">

                <div id="photo" class="idBox">                                            
                    <p>上传照片</p>
                    <img id="imgs" src="img/photo/replace.png">
                    <input type="file" onchange="previewImage(this,1)">                                        
                </div>
                <div id="photo1" class="idBox">                                           
                    <p>上传照片</p>                                        
                    <img id="imgs1" src="img/photo/replace.png">
                    <input type="file" onchange="previewImage(this,2)">                     
                </div>
                <div class="confirmBt"><input type="submit" value="确认提交"></div>
            </div>              
        </div><!-- / #mainVisual -->        
    </div><!-- / #page -->
</body>
</html>
  • 写回答

1条回答 默认 最新

  • july_apple 2016-01-30 13:59
    关注

    什么叫不能完整覆盖?

    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码