每次上传图片都不能完整的将图片覆盖,请求各路大神帮帮忙
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>