背景
我在网站&告诉用户显示面具图片上传图片....
一旦用户上传图片,我填写用户内侧面罩图像上传图像
在website和在codepen其做工精细:https://codepen.io/kidsdial/pen/wNqXEJ
要求:
我现在想上传二个掩模图像,但这次面具图片不显示在codepen2:https://codepen.io/kidsdial/pen/OdjErW和在website2
HTML
<input type="file" id="fileupa" />
<input type="file" id="fileupb" />
<div class="minaimg masked-imga" ondragover="onDragOver(event)"ondragover="onDragOver(event)" >
<div draggable="true" ondragstart="onDragStart(event)" id="uploadedImg">
<div class="minaimga">
<!-- give an id for ease of selection -->
<img id="target_img" alt="">
<div></div>
</div>
</div>
</div>
<div class="minaimg masked-imgb" ondragover="onDragOver(event)"ondragover="onDragOver(event)" >
<div draggable="true" ondragstart="onDragStart(event)" id="uploadedImg">
<div class="minaimgb">
<!-- give an id for ease of selection -->
<img id="target_img" alt="">
<div></div>
</div>
</div>
</div>
CSS
.masked-imga {
-webkit-mask-image: url(https://i.stack.imgur.com/EQpM4.png);
mask-image: url(https://i.stack.imgur.com/EQpM4.png);
-webkit-mask-position: center center;
mask-position: center center;
-webkit-mask-repeat: no-repeat;
mask-repeat: no-repeat;
display: block;
width: 100%;
height: 100%;
}
.minaimga {
display: block;
background-color: black;
}
.masked-imgb {
-webkit-mask-image: url(https://i.stack.imgur.com/EQpM4.png);
mask-image: url(https://i.stack.imgur.com/EQpM4.png);
-webkit-mask-position: center center;
mask-position: center center;
-webkit-mask-repeat: no-repeat;
mask-repeat: no-repeat;
display: block;
width: 100%;
height: 100%;
}
.minaimgb {
display: block;
background-color: black;
}
脚本
fileupa.onchange = e => {
target_img.src = URL.createObjectURL(fileupa.files[0]);
}
fileupb.onchange = e => {
target_img.src = URL.createObjectURL(fileupb.files[0]);
}
let prevX = 0, prevY = 0,translateX = 0, translateY = 0, scale = 1, zoomFactor = 0.1;
function onDragStart(evt) {
if (evt.dataTransfer && evt.dataTransfer.setDragImage) {
evt.dataTransfer.setDragImage(evt.target.nextElementSibling, 0, 0);
}
prevX = evt.clientX;
prevY = evt.clientY;
}
function onDragOver(evt) {
translateX += evt.clientX - prevX;
translateY += evt.clientY - prevY;
prevX = evt.clientX;
prevY = evt.clientY;
updateStyle();
}
function updateStyle()
{
let transform = "translate(" +translateX+ "px, "+ translateY + "px) scale("+scale+")";
if(document.querySelector('#uploadedImg img'))
document.querySelector('#uploadedImg img').style.transform = transform;
}
在你的代码的问题您使用id
标签img
相同target_img
。 IDs
are始终是唯一的。
文件上传一
<div class="minaimga">
<!-- give an id for ease of selection -->
<img id="target_imga" alt="">
<div></div>
</div>
文件上传B:
<div class="minaimgb">
<!-- give an id for ease of selection -->
<img id="target_imgb" alt="">
<div></div>
</div>
fileupa.onchange = e => {
target_imga.src = URL.createObjectURL(fileupa.files[0]);
}
fileupb.onchange = e => {
target_imgb.src = URL.createObjectURL(fileupb.files[0]);
}
您可以将height
这两类.minaimga,.minaimgb
。
.minaimga,.minaimgb {
display: block;
background-color: black;
height:500px;
}
您不能使用一个ID两次。 ID始终是唯一的。你应该使用类。