Posted on: 11.08.2020 Posted by: Alex D Comments: 1

Nice Accordion menu on CSS and jQuery

The structure of the HTML menu accordion.

<ul class="accordion">
  <li>
    <h4>My sites</h4>
    <div>
      <P>1P</P><P>2P</P><P>3P</P>
    </div>
  </li>
  <li>
    <h4>Personal data</h4>
    <div>
      <P>1P</P><P>2P</P><P>3P</P>
    </div>
  </li>
  <li>
    <h4>Information</h4>
    <div>
      <P>1P</P><P>2P</P><P>3P</P>
    </div>
  </li>
  <li>

You have to add these CSS properties for this HTML code.

.accordion {
  width: 500px;
  margin: 0 auto 100px;
  border-top: 1px solid #d9e5e8;
}
.accordion li {
  border-bottom: 1px solid #d9e5e8;
  position: relative;
}
.accordion li div {
  display: none;
  color: black;
}
.accordion h4 {
  width: 100%;
  display: block;
  cursor: pointer;
  font-weight: 600;
  line-height: 3;
  font-size: 14px;
  font-size: 0.875rem;
  text-indent: 15px;
  user-select: none;
}
.accordion h4:after {
  width: 8px;
  height: 8px;
  border-right: 1px solid #4a6e78;
  border-bottom: 1px solid #4a6e78;
  position: absolute;
  right: 10px;
  content: " ";
  top: 17px;
  transform: rotate(-45deg);
  -webkit-transition: all 0.2s ease-in-out;
  -moz-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}
.accordion div {
  font-size: 17px;
  line-height: 2;
  padding: 10px;
}
h4.active:after {
  transform: rotate(45deg);
  -webkit-transition: all 0.2s ease-in-out;
  -moz-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}

You have to add the jQuery code. Place it below the HTML code.

$(".accordion h4").click(function(j) {
    var dropDown = $(this).closest("li").find("div");

    $(this).closest(".accordion").find("div").not(dropDown).slideUp();

    if ($(this).hasClass("active")) {
        $(this).removeClass("active");
    } else {
        $(this).closest(".accordion").find("h4.active").removeClass("active");
        $(this).addClass("active");
    }

    dropDown.stop(false, true).slideToggle();

    j.preventDefault();
});

Sample code

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<STYLE>
.accordion {
  width: 500px;
  margin: 0 auto 100px;
  border-top: 1px solid #d9e5e8;
}
.accordion li {
  border-bottom: 1px solid #d9e5e8;
  position: relative;
}
.accordion li div {
  display: none;
  color: black;
}
.accordion h4 {
  width: 100%;
  display: block;
  cursor: pointer;
  font-weight: 600;
  line-height: 3;
  font-size: 14px;
  font-size: 0.875rem;
  text-indent: 15px;
  user-select: none;
}
.accordion h4:after {
  width: 8px;
  height: 8px;
  border-right: 1px solid #4a6e78;
  border-bottom: 1px solid #4a6e78;
  position: absolute;
  right: 10px;
  content: " ";
  top: 17px;
  transform: rotate(-45deg);
  -webkit-transition: all 0.2s ease-in-out;
  -moz-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}
.accordion div {
  font-size: 17px;
  line-height: 2;
  padding: 10px;
}
h4.active:after {
  transform: rotate(45deg);
  -webkit-transition: all 0.2s ease-in-out;
  -moz-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}
</STYLE>
<ul class="accordion">
  <li>
    <h4>My sites</h4>
    <div>
      <P>1P TEXT</P><P>2P TEXT</P><P>3P TEXT</P>
    </div>
  </li>
  <li>
    <h4>Personal data</h4>
    <div>
      <P>1P TEXT</P><P>2P TEXT</P><P>3P TEXT</P>
    </div>
  </li>
  <li>
    <h4>Information</h4>
    <div>
      <P>1P TEXT</P><P>2P TEXT</P><P>3P TEXT</P>
    </div>
  </li>
  <li>
    <h4>Partner</h4>
    <div>
      <P>1P TEXT</P><P>2P TEXT</P><P>3P TEXT</P>
    </div>
  </li>
  <li>
    <h4>Pay</h4>
    <div>
      <P>1P TEXT</P><P>2P TEXT</P><P>3P TEXT</P>
    </div>
  </li>
</ul>
<SCRIPT>
///////// This line automatically opens the first block h4 ///////////////
$(".accordion > li:eq(0) h4").addClass("active").next().slideDown();
$(".accordion h4").click(function(j) {
    var dropDown = $(this).closest("li").find("div");
    $(this).closest(".accordion").find("div").not(dropDown).slideUp();
    if ($(this).hasClass("active")) {
        $(this).removeClass("active");
    } else {
$(this).closest(".accordion").find("h4.active").removeClass("active");
        $(this).addClass("active");
    }
    dropDown.stop(false, true).slideToggle();
    j.preventDefault();
});
</SCRIPT>

DEMO

Categories:

1 people reacted on this

Leave a Reply to sashad.ru Cancel reply