Accordion - How to make it plugin - Forum

Forum Navigation
You need to log in to create posts and topics.

Accordion - How to make it plugin

Hi..

I am trying to create a plugin from the following code and i need some help on where i should put the css, how i will prompt the user to add the Container and so on.

<style>
.accordion {
  background-color: #888899;
  color: #FFFFFF;
  cursor: pointer;
  padding: 15px;
  width: 100%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
  transition: 0.4s;
}

.active, .accordion:hover {
  background-color: #87CEEB;
}

.accordion:after {
  content: '\2BC6';
  color: #FFFFFF;
  font-size: 15px;
  font-weight: bold;
  float: right;
  margin-left: 7px;
}

.active:after {
  content: "\2BC5";
}

.panel {
  padding: 0 15px;
  text-align: justify;
  background-color: white;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
}
</style>

<button class="accordion">Section 1</button>
<div class="panel">
  <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>

<button class="accordion">Section 2</button>
<div class="panel">
  <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>

<button class="accordion">Section 3</button>
<div class="panel">
  <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>

<button class="accordion">Section 3</button>
<div class="panel">
  <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>

<script>
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.maxHeight) {
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    }
  });
}
</script>

I took it from:

https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_accordion_symbol

I also notice that when i put the code in a container in VNW there is a gap between sections.

Thanks

@smartmedia I have had to rename the .panel CSS class to .accordion-panel as it conflicts with the BootStrap CSS class.
Please take a look at my attached sample app.
Creating a plugin requires injecting the HTML code into the Container and it's certainly not easy to explain here.
Anyway the code is interesting and very simple. If I get some time will try to create the plugin.

Thank you!

Uploaded files:
  • You need to login to have access to uploads.
smartmedia has reacted to this post.
smartmedia

Update.

I have add two buttons that open and close all sections at once.

Uploaded files:
  • You need to login to have access to uploads.
luishp has reacted to this post.
luishp