简体   繁体   English

如何在html / css / javascript中动态创建两排正方形

[英]How to dynamically create two rows of squares in html/css/javascript

I'm trying to create 2 rows of 12 rectangles (considered to be one object). 我正在尝试创建2行,每行12个矩形(被认为是一个对象)。

I also want to add a plus button, so that when the user clicks on either end, a new set of rectangles appear above or below the original ones. 我还想添加一个加号按钮,以便当用户单击任一端时,一组新的矩形会出现在原始矩形的上方或下方。 (depending on which plus button they click on). (取决于他们点击哪个加号按钮)。 I think I've got both plus buttons working. 我想我的两个加号按钮都起作用了。

So I am trying to achieve the following: 因此,我试图实现以下目标:

http://i.stack.imgur.com/P26sC.png http://i.stack.imgur.com/P26sC.png

What I have done so far: 到目前为止,我所做的是:

https://jsfiddle.net/zp5hnwmx/ https://jsfiddle.net/zp5hnwmx/

 $(function () {
$("body").on('click', ".repeat", function (e) {
    e.preventDefault();
    var $self = $(this);
    var $parent = $self.parent();
    if($self.hasClass("add-bottom")){
      $parent.after($parent.clone());
    } else {
      $parent.before($parent.clone());
    }
});
});

I am currently facing a bunch of issues: 我目前面临很多问题:

When I run my code locally, the rectangles are not aligned properly and they wrap around to the next line (I want 2 rows of 12). 当我在本地运行代码时,矩形未正确对齐,它们环绕到下一行(我想要2行12)。 This is what I see: 这是我看到的:

http://s16.postimg.org/ro0uzbrdx/Capture.png http://s16.postimg.org/ro0uzbrdx/Capture.png

After creating the rectangles, how can I access them individually? 创建矩形后,如何单独访问它们?

Does the following addition to your css help? CSS的以下补充内容是否有帮助?

.repeat {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
}
.repeat > * {
  flex: 0 0 30px;
}

Try adding white-space: nowrap; 尝试添加white-space: nowrap; to whichever div is holding your square elements. 到哪个div拥有您的方形元素。 Of course, this will create some overflow if the page is small, which will need to be handled by a parent element with overflow: auto; 当然,如果页面很小,这会产生一些溢出,这需要由具有overflow: auto;的父元素来处理overflow: auto; or some such statement. 或某些此类陈述。

Edit: 编辑:
As for: 至于:

After creating the rectangles, how can I access them individually? 创建矩形后,如何单独访问它们?

Whether or not attaching event listeners to each square is inefficient will depend on the performance requirements of your system. 将事件侦听器附加到每个方框的效率是否低下取决于系统的性能要求。 And the debates continue as to which is the most efficient structure. 关于哪种结构最有效的争论还在继续。 Now, this is purely subjective (as I only have a limited view of your project) and I do recommend you do further research... but I'd say that attaching one event listener to the most immediate parent container that wraps all your squares would be the best idea. 现在,这纯粹是主观的(因为我对您的项目只有有限的看法),我建议您做进一步的研究……但是我要说的是,将一个事件侦听器附加到包裹所有正方形的最直接父容器最好的主意。

Since you can have an unknown amount of squares (potentially hundreds or thousands), you don't want to attach event listeners to each square. 由于您可以拥有数量未知的正方形(可能是数百个或数千个),因此您不想将事件侦听器附加到每个正方形。 That's a huge memory sink of duplicate code. 这是重复代码的巨大内存。 Instead, doing event listener delegation would be far better for memory efficiency. 取而代之的是,进行事件侦听器委派将大大提高内存效率。 There will be a tiny bit more time waste but it should be O(1) and therefore negligible. 将会浪费一点时间,但是应该是O(1),因此可以忽略不计。

In summary, applying one event listener to a deep parent element (for example <div class="container"> ) would likely be the most efficient method for you. 总之,将一个事件侦听器应用于深父元素(例如<div class="container"> )对于您而言可能是最有效的方法。

Resources: 资源:
https://www.kirupa.com/html5/handling_events_for_many_elements.htm http://davewasmer.com/javascript-event-delegation/ https://www.kirupa.com/html5/handling_events_for_many_elements.htm http://davewasmer.com/javascript-event-delegation/

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

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