简体   繁体   English

手风琴的条件打开和关闭功能

[英]Accordion conditional open and close function

I implemented a simple accordion menu for my mobile view with clickable images as menu headers. 我为移动视图实现了一个简单的手风琴菜单,其中可点击的图像作为菜单标题。

I would like to have only one category open at a time, basically, a click on picture 2 should open the picture 2 accordion content body, but close all the other accordion content bodies. 我想一次只打开一个类别,基本上,单击图片2应该打开图片2的手风琴内容主体,而关闭所有其他手风琴内容主体。 Right now I would need to click on the each picture again to close its body. 现在,我需要再次单击每张图片以关闭其主体。

Hope that makes sense. 希望有道理。

This is my code thus far: 到目前为止,这是我的代码:

 var acc = document.getElementsByClassName("accordion"); var i; for (i = 0; i < acc.length; i++) { acc[i].addEventListener("click", function() { this.classList.toggle("active"); var panel = this.nextElementSibling; if (panel.style.display === "block") { panel.style.display = "none"; } else { panel.style.display = "block"; } }); } 
 .accordion { background-color: #fff; color: #444; cursor: pointer; width: 103%; border: none; text-align: left; outline: none; font-size: 15px; transition: 0.4s; margin: -5px; } .bg { width: 100%; } .active, .accordion:hover { background-color: #fff; } .panel { padding: 0 0px; display: none; width: 100%; background-color: white; overflow: hidden; } 
 <button class="accordion"><div class="bolimg"> <img class="bg" src="/files/0.jpg"> </div></button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion"><img class="bg" src="/files/1.jpg"></button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion"><img class="bg" src="/files/2.jpg"></button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion"><img class="bg" src="/files/3.jpg"></button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion"><img class="bg" src="/files/4.jpg"></button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion"><img class="bg" src="/files/5.jpg"></button> <div class="panel"> <p>Content body text</p> </div> 

This is a little script i can come up with. 这是我可以想到的一个小脚本。

I added the next css class to control the state of the accordion and to avoid doing the css with js: 我添加了下一个CSS类来控制手风琴的状态,并避免使用js进行CSS:

.accordion.active+div {
  display: block
}

The script validate two things 该脚本验证两件事

  1. If there is one open al ready 如果有一个开放的准备好了
  2. If the one clicked is the same as the one that is opened 如果单击的与打开的相同

I hope this help, if you need anything else i'll be around 希望对您有所帮助,如果您还有其他需要,我会在身边

 var acc = document.getElementsByClassName("accordion"); var i; var open = null; for (i = 0; i < acc.length; i++) { acc[i].addEventListener("click", function() { if (open == this) { open.classList.toggle("active"); open = null; } else { if (open != null) { open.classList.toggle("active"); } this.classList.toggle("active"); open = this; } }); } 
 .accordion { background-color: #fff; color: #444; cursor: pointer; width: 103%; border: none; text-align: left; outline: none; font-size: 15px; transition: 0.4s; margin: -5px; } .bg { width: 100%; } .active, .accordion:hover { background-color: #fff; } .panel { padding: 0 0px; display: none; width: 100%; background-color: white; overflow: hidden; } .accordion.active+div { display: block } 
 <button class="accordion"><div class="bolimg"> <img class="bg" src="/files/0.jpg"> </div></button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion"><img class="bg" src="/files/1.jpg"></button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion"><img class="bg" src="/files/2.jpg"></button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion"><img class="bg" src="/files/3.jpg"></button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion"><img class="bg" src="/files/4.jpg"></button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion"><img class="bg" src="/files/5.jpg"></button> <div class="panel"> <p>Content body text</p> </div> 

If you are using jQuery library, try to make use of 如果您使用的是jQuery库,请尝试利用

Note: I have removed the images from your code and places just numbers for visuals 注意:我已从您的代码中删除了图片,并只添加了视觉效果编号

Stack Snippet 堆栈片段

 $(".accordion").on("click", function() { $(".panel").removeClass("show"); $(this).addClass("active").siblings(".accordion").removeClass("active") $(this).next(".panel").addClass("show"); }); 
 .accordion { background-color: #fff; color: #444; cursor: pointer; width: 103%; border: none; text-align: left; outline: none; font-size: 15px; transition: 0.4s; margin: -5px; } .bg { width: 100%; } .active, .accordion:hover { background-color: #000; color: #fff; } .panel.show { display: block } .panel { padding: 0 0px; display: none; width: 100%; background-color: white; overflow: hidden; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="accordion">1</button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion">2</button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion">3</button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion">4</button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion">5</button> <div class="panel"> <p>Content body text</p> </div> <button class="accordion">6</button> <div class="panel"> <p>Content body text</p> </div> 

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

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