简体   繁体   English

用透明度叠加两个图像

[英]Overlay of two images with transparency

I'm looking for a solution in Javascript maybe to create a real-time preview of products based on multiple options that a consumer could choose.我正在寻找 Javascript 中的解决方案,可能是基于消费者可以选择的多个选项创建产品的实时预览。

Exemple: I have a face.png and a hat.png with transparents parts, and a cap, and when I click on cap, I would like to display the image of the cap + the face and if I click on hat, I would like to update my preview in real time and keep the face and only change the image of the hat (a png with transparency)示例:我有一个带有透明部分的 face.png 和一个 hat.png 以及一个帽子,当我点击帽子时,我想显示帽子 + 脸部的图像,如果我点击帽子,我会喜欢实时更新我的预览并保留脸部并且只更改帽子的图像(具有透明度的png)

Do you have any idea how I could do this?你知道我该怎么做吗?

Thanks in advance !提前致谢 !

you can use a container div and place face and hat inside of that div then position each other with position absolute您可以使用容器 div 并将面部和帽子放在该 div 内,然后 position 与 position 绝对

i added a function for changing hats all hats where transparent at the beginning then we change opacity of the first hat inside of js我添加了一个 function 用于更改帽子所有在开始时透明的帽子然后我们更改 js 内部第一个帽子的不透明度

by clicking next button we change each hat by setting all hats to transparent again and then bringing back our current hat's opacity to 1通过单击下一步按钮,我们通过再次将所有帽子设置为透明来更改每个帽子,然后将我们当前帽子的不透明度恢复为 1

 let hats = Array.from(document.querySelectorAll(".hat")); let currentIndex = 0; hats[currentIndex].style.opacity = 1; function next(){ hats.forEach((hat)=> hat.style.opacity = 0); hats[currentIndex].style.opacity = 1; currentIndex = (currentIndex === hats.length -1)? 0: currentIndex+1; } document.querySelector('#next').addEventListener('click',next)
 .pic{ position:relative; width:200px; height:300px; border:1px solid black; }.face{ position: absolute; height: 150px; width:100px; background:red; bottom:25px; left:50px; }.hat{ width:180px; height:50px; position:absolute; left:10px; bottom:150px; opacity:0; }.pink{ background:#ffc0cba6; }.black{ background:#000000a6; }.yellow{ background:#ffff00a6; }.green{ background:#00ff00a6; }
 <div class="pic"> <div class="face"></div> <div class="hat green"></div> <div class="hat yellow"></div> <div class="hat pink"></div> <div class="hat black"></div> </div> <button id="next">next</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM