简体   繁体   English

如何在图像的不同位置添加多个叠加层?

[英]How to add multiple overlays at different positions in an image?

 .reddot { height: 10px; width: 10px; background-color: #D03C3C; border-radius: 10%; display: inline-block; position: absolute; top: -5px; left:195px; }.reddot_2 { height: 10px; width: 10px; background-color: #D03C3C; border-radius: 10%; display: inline-block; position: absolute; top: 95px; left:135px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div style="position: relative; left: 0; top: 0;"> <img src="https://i.ibb.co/z5BV8j6/l.png" alt="Map" class="img-fluid" width="200px" height="200px"> <div class="reddot"></div> <div class="reddot_2"></div> </div>

How to add and delete multiple overlays (Red Rectangles) using simple buttons to an image and the adding of overlays would have a simple text box of top and left pixel input as shown in the CSS so that the red rectangles can be added to the position I inputted into the textbox?如何使用简单的按钮向图像添加和删除多个叠加层(红色矩形),添加叠加层将有一个简单的顶部和左侧像素输入文本框,如 CSS 所示,以便可以将红色矩形添加到 position我输入到文本框? Any help will be appreciated:)任何帮助将不胜感激:)

Thank you for reading and have a nice day!感谢您的阅读,祝您有美好的一天!

For now, I am writing multiple red-dot classes to be overlay with the image but it is tedious.现在,我正在编写多个红点类来覆盖图像,但这很乏味。 <div class="reddot"></div>

You should use Javascript to add and remove the rectangles dynamically when the buttons are pressed.您应该使用 Javascript 在按下按钮时动态添加和删除矩形。 Here are an example demonstrating how to add some rectangles based on a fixed list of positions.下面是一个示例,演示如何根据固定的位置列表添加一些矩形。 At least with the following code, you will not have to write multiple red-dot classes.至少有了下面的代码,你就不用写多个红点类了。

 document.addEventListener("DOMContentLoaded", function(e) { const reddotContainer = document.getElementById('reddot-container'); [{top: '-5px', left: '195px'}, {top: '95px', left: '135px'}, {top: '50px', left: '50px'}, {top: '10px', left: '135px'}].forEach(reddotPosition => { const newReddot = document.createElement('div'); newReddot.className = 'reddot'; newReddot.style.left = reddotPosition.left; newReddot.style.top = reddotPosition.top; reddotContainer.append(newReddot); }); });
 .reddot { height: 10px; width: 10px; background-color: #D03C3C; border-radius: 10%; display: inline-block; position: absolute; }
 <div style="position: relative; left: 0; top: 0;" id="reddot-container"> <img src="https://i.ibb.co/z5BV8j6/l.png" alt="Map" class="img-fluid" width="200px" height="200px"> </div>

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

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