简体   繁体   English

如何在HTML和CSS中实现具有可折叠组件的粘性划分?

[英]How to implement sticky division having collapsible component in HTML & CSS?

I am trying to merge the "div" tag containing the a "p" tag, a collapsible button with the HTML table header such that when the user scrolls all the components above the HTML table body data will stick to the top which is working. 我正在尝试合并包含“ p”标签的“ div”标签,这是一个带有HTML表标题的可折叠按钮,以便当用户滚动HTML表主体数据上方的所有组件时,它们会停留在顶部,这是有效的。 The issue is when I am expanding the collapsible button it overlaps the whole table instead of shifting it down. 问题是,当我展开可折叠按钮时,它与整个表格重叠,而不是向下移动。 May be the issue is due to z-axis. 可能是由于z轴引起的。 But I want everything to stick on the top position and avoid overlapping of the expansion area. 但我希望一切都停留在最高位置,并避免扩展区域重叠。 Any help would is appreciated. 任何帮助将不胜感激。

 var coll = document.getElementsByClassName("collapsible"); var i; for (i = 0; i < coll.length; i++) { coll[i].addEventListener("click", function() { this.classList.toggle("active"); var content = this.nextElementSibling; if (content.style.display === "block") { content.style.display = "none"; } else { content.style.display = "block"; } }); } 
 .stickyDiv { position: -webkit-sticky; position: sticky; z-index: 99; top: -1px; background-color: #DDD; border: initial; } table.floatThead-table { border-top: none; border-bottom: none; background-color: #fff; } th { position: -webkit-sticky; position: sticky; top: 120px; background-color: #0B2D71; color:#fff; } .collapsible { background-color: #777; color: white; cursor: pointer; padding: 18px; width: 100%; border: none; text-align: left; outline: none; font-size: 15px; } .active, .collapsible:hover { background-color: #555; } .content { padding: 0 18px; display: none; overflow: hidden; background-color: #f1f1f1; } 
 <h1>Sticky Collapsible</h1> <div class='stickyDiv'> <p> Lorem Ipsum.............</p> <button class="collapsible">Open Collapsible</button> <div class="content"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </div> </div> <div> <table id="tbl"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Points</th> </tr> </thead> <tbody> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr><tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr><tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr><tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr><tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr><tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr><tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr><tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr><tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr><tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr><tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> </tbody> </table> </div> 

Rather than manipulating an element's style attribute: 而不是操纵元素的style属性:

content.style.display === 'block';

It's definitely better practice to add and remove classes: 添加和删​​除类绝对是更好的做法:

content.classList.add('block');
content.classList.remove('block');

You can then style the class in your stylesheet: 然后,您可以在样式表中设置类的样式:

.block {
  display: block;
}

But you can also influence immediately subsequent siblings: 但是您也可以立即影响后续的同级兄弟:

.block + .my-sibling {
  [STYLES HERE]
}

and any other siblings further on: 以及其他兄弟姐妹:

.block ~ .my-sibling {
  [STYLES HERE]
}

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

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