面膜不显示图像多次使用时

问题描述 投票:0回答:2

背景

我在网站&告诉用户显示面具图片上传图片....

enter image description here

一旦用户上传图片,我填写用户内侧面罩图像上传图像

enter image description here

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;
}
javascript html css html5 css3
2个回答
0
投票

在你的代码的问题您使用id标签img相同target_imgIDsare始终是唯一的。

文件上传一

 <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>

Working Demo

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;
    }

-3
投票

您不能使用一个ID两次。 ID始终是唯一的。你应该使用类。

© www.soinside.com 2019 - 2024. All rights reserved.