简体   繁体   English

仅单击按钮即可折叠/关闭按钮

[英]Collapse/close a button only by clicking on it

I'm trying to code a responsive button which can be placed multiple times in the same line, showing its content always below the line containing the button. 我正在尝试编写一个可以在同一行中多次放置的响应式按钮,以始终将其内容显示在包含该按钮的行下方。

In the snippet there is a working code, but it has a small flaw: since the pseudo-class focus is used, once the button is opened it's enough to click anywhere on the screen to close it. 该代码段中有一个有效的代码,但有一个小缺陷:由于使用了伪类focus ,因此一旦打开按钮,就足以单击屏幕上的任何位置以将其关闭。

The usual behaviour for a button is that to close it you have to click on it, so is it possibile to get this behaviour also for this one? 一个按钮的通常行为是要关闭它,您必须单击它,所以对于这个按钮,是否也有可能获得这种行为?

I used other pseudo-classes but without success, I guess only a javascript can do the job. 我使用了其他伪类,但没有成功,我猜想只有JavaScript才能完成这项工作。

 .container { position: relative; margin: 2em; } .details { display: none; } .collapsible:focus { margin-bottom: 1.5em; pointer-events: none; } .collapsible:focus + .details { display: block; margin-top: -1.15em; position: absolute; left: 0; right: 0; background: yellow; } 
 <div class=container> You can <button class="collapsible">place</button><span class=details>yes</span> more than <button class="collapsible">one</button><span class=details>nice</span> per line, they are responsive and the content is always shown just <button class="collapsible">below</button><span class=details>cool</span> the line containing the button. But once opened, you can close it with a click <button class="collapsible">everywhere</button><span class=details>not good</span> on the screen </div> 

Javascript for further customization 进一步定制的Javascript

<script type="text/javascript">
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.parentElement.nextElementSibling;
    if (content.style.maxHeight){
      content.style.maxHeight = null;
    } else {
      content.style.maxHeight = content.scrollHeight + "px";
    } 
  });
}
</script>

Implementation of the idea was a bit more complicated so I'll just answer. 这个想法的实现要复杂一些,所以我就回答。

This uses an old trick whereby a label, associated with a hidden checkbox, is used as the click target. 这使用了一个古老的技巧,将与隐藏的复选框关联的标签用作点击目标。 Since clicking on a label checks or unchecks the checkbox, and there is a pseudo-class for the checked state of the checkbox, we can use that to persist the state of our styles. 由于单击标签会选中或取消选中复选框,并且复选框的选中状态有一个伪类,因此我们可以使用它来保留样式的状态。 Credit to TylerH for his answer to the similar question Can I have an onclick effect in CSS? 感谢TylerH对类似问题的回答我可以在CSS中产生onclick效果吗? .

I've implemented it here by using a partial attribute selector, so in this example any checkboxes have to have an ID that begins with "demo". 我在这里是通过使用部分属性选择器实现的,因此在此示例中,所有复选框都必须具有以“ demo”开头的ID。 The checkboxes do have to have an ID for the for attribute of the label to hook onto. 复选框必须具有一个ID,以使labelfor属性挂接到钩子上。

 .container { position: relative; margin: 2em; } .collapsible:focus { margin-bottom: 1.5em; pointer-events: none; } label { display: inline-block; background: lightgrey; } [id^="demo"] { display: none; } /* details that are next to labels that are next to unchecked checkboxes are hidden */ [id^="demo"]:not(:checked)+label+.details { display: none; } /* details that are next to labels that are next to checked checkboxes are displayed */ [id^="demo"]:checked+label+.details { display: block; position: absolute; left: 0; right: 0; background: yellow; } /* labels that are next to unchecked checkboxes have a different color so you can track which ones are "on" */ [id^="demo"]:checked+label { background: blue; color: white; } 
 <div class=container> You can <input type="checkbox" id="demo01" /><label for="demo01" >place</label><span class=details>yes</span> more than <input type="checkbox" id="demo02" /><label for="demo02">one</label><span class=details>nice</span> per line, they are responsive and the content is always shown just <input type="checkbox" id="demo03" /><label for="demo03">below</label><span class=details>cool</span> the line containing the button. But once opened, you can close it with a click <input type="checkbox" id="demo04" /><label for="demo04">everywhere</label><span class=details>not good</span> on the screen </div> 

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

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