简体   繁体   English

如何在浮动div列表下方插入div

[英]How to insert div underneath list of floating divs

Say you have 5-20 inner boxes inside an outer box, which has a width of 300-500 pixels. 假设您在一个外部框内有5-20个内部框,外部框的宽度为300-500像素。

<outer>
<inner>Number 1</inner>
<inner>Number 2</inner>
<inner>Number 3</inner>
<inner>Number 4</inner>
<inner>Number 5</inner>
<inner>Number 6</inner>
...
</outer>

Depending on the outer width, they could end up being displayed like this: 根据外部宽度,它们最终可能会像这样显示:

Number 1  Number 2
Number 3  Number 4
Number 5  Number 6

Or this: 或这个:

Number 1  Number 2  Number 3
Number 4  Number 5  Number 6

Now, when you tap an inner box (eg. #2), you want to reveal a new box underneath the tapped box: 现在,当您点击一个内部框(例如,#2)时,您想要在被点击的框下面显示一个新框:

Number 1  Number 2
+----------------+
|                |
| Hello Number 2 |
|                |
+----------------+
Number 3  Number 4
Number 5  Number 6

Or if the width is larger, it would look like this: 或者,如果宽度较大,则看起来像这样:

Number 1  Number 2  Number 3
+--------------------------+
|                          |
|      Hello Number 2      |
|                          |
+--------------------------+
Number 4  Number 5  Number 6

My question is, in order to be able to easily insert the new box in the correct place in markup, what is the best way to lay out the inner boxes? 我的问题是,为了能够轻松地在标记中的正确位置插入新框,布置内部框的最佳方法是什么?

a) Simply float the inner boxes. a)只需将内盒浮起即可。 Good: Browser handles positioning. 良好:浏览器可以处理定位。 Bad: How do you determine where to insert the new box when your code doesn't know the positions of the inner boxes? 不好:当您的代码不知道内部框的位置时,如何确定在哪里插入新框?

b) Have your code position the boxes in columns and rows according to the available width. b)让您的代码根据可用宽度将框定位在列和行中。 Good: Easy to insert the new box the right place. 良好:容易在正确的位置插入新盒子。 Problem: Creates challenges with responsiveness. 问题:以响应能力创造挑战。

Example: See how album details are revealed in iTunes when clicking an album thumbnail 示例:查看单击专辑缩略图时如何在iTunes中显示专辑详细信息

I hope i got your question right, you need to change the css when needed , for example add a class that has float:none and clear:both when a condition (like click occurs. 我希望我的问题正确无误,您需要在需要时更改css,例如,添加一个具有float:none和clear:both的类(当发生条件(如单击)时)。

I did an example on fiddle: https://jsfiddle.net/ejeqs8m7/2/ Just click on any of the divs. 我在小提琴上做了一个示例: https : //jsfiddle.net/ejeqs8m7/2/只需单击任何div。

It toggles a class .clicked that looks like this: 它切换一个看起来像这样的类.clicked:

.clicked {
  float:none;
  clear:both;
  width:auto;
}

You can check whether the elements are in same horizontal line or not and add the viewer element dynamically using JavaScript. 您可以检查元素是否在同一水平线上,并使用JavaScript动态添加查看器元素。

 var idles = document.querySelectorAll('.idle'); Array.prototype.forEach.call(idles, function(idle) { idle.addEventListener('click', showInfo); }); function showInfo(e) { var selectedTabClass = 'active'; var viewerClass = 'viewer'; if (showInfo.prevClickElement && showInfo.prevClickElement !== this) { showInfo.prevClickElement.classList.remove(selectedTabClass); } if (this.classList.contains(selectedTabClass)) { // remove viewer element showInfo.viewer.parentElement.removeChild(showInfo.viewer); this.classList.remove(selectedTabClass); return; } if (!showInfo.viewer) { // Create a viewer element only if it wasn't created before showInfo.viewer = document.createElement('div'); showInfo.viewer.classList.add(viewerClass); } if (!showInfo.section) { // Get the parent container showInfo.section = document.querySelector('section'); } var after = this; // Getting the next element which isn't in the exact horizontal line with the clicked element while (after && after.offsetTop === this.offsetTop) { after = after.nextElementSibling; } // Add the content through the below line showInfo.viewer.innerHTML = this.innerHTML + " Viewer"; if (after) { showInfo.section.insertBefore(showInfo.viewer, after); } else { // if after element isn't present then safely assuming that the clicked element was in the last horizontal line and appending the viewer element showInfo.section.appendChild(showInfo.viewer); } this.classList.add(selectedTabClass); showInfo.prevClickElement = this; } 
 section { width: 200px; } .idle { background-color: green; float: left; width: 100px; } .clicked { background-color: yellow; float: none; clear: both; width: auto; } .viewer { clear: both; } 
 <section> <div class="idle">Div #1</div> <div class="idle">Div #2</div> <div class="idle">Div #3</div> <div class="idle">Div #4</div> <div class="idle">Div #5</div> <div class="idle">Div #6</div> <div class="idle">Div #7</div> <div class="idle">Div #8</div> </section> 

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

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