javascript - 如何在 javascript 中基于鼠标单击重叠 2 个图像?
问题描述
期望的行为是能够在网格中单击,并且覆盖的图像将保持在正确的正方形中。但是,在调整浏览器窗口大小时,定位会变得混乱(参见下面的链接)。
问题演示链接:https ://www.youtube.com/watch?v=4fbPlQLvHLs
function onClick(e) {
let xPos = e.clientX;
let yPos = e.clientY;
var newImg = document.createElement("img");
newImg.src = 'https://www.pngmart.com/files/4/Circle-PNG-File.png';
newImg.style.position = "absolute";
newImg.style.left = xPos + "px";
newImg.style.top = yPos + "px";
newImg.style.width = "35px";
document.getElementById("wrapper").appendChild(newImg);
<div class="wrapper" id="wrapper">
<img id="my-image" src="https://www.guineapigcagesstore.com/resize/Shared/Images/Product/Standard-Grid/standard-grid.jpg?bw=1000&w=1000&bh=1000&h=1000"
alt="img" width="300px" position="relative" ></img>
</div>
包装样式:
.wrapper {
text-align: center;
}
解决方案
推荐阅读
- python - 使用 csv(标签)为 CNN 加载图像?
- android - mp android(折线图)Iaxisvalue 格式化程序导致 ArrayIndexOutOfBoundsException: length=10; 指数=-1
- active-directory - Azure AD 全球 + 德国 - 一个域
- node.js - Electron App 中的事件发射器内存泄漏
- arrays - 在 typescript 中查找 3 个数组中的公共元素
- c# - 如何从元组列表中提取 item2(Value) 列表
>? - python-3.x - 如何将python函数包装成超时
- python - 如果其他列包含字符串,则基于定义的列表创建列
- mysql - 识别独特的字符
- enums - 我可以限制 Rust 枚举的大小吗?