Skip to content

Instantly share code, notes, and snippets.

@kuri65536
Last active February 12, 2018 03:56
Accordion list with CSS

live sample in bl.ocks.org

There are some tips in this sample.

  • CSS: nth-child and it's ranging
  • CSS: transition opacity and top properties. (I tested with display property, too)
  • CSS: override nth-child properties by :not() peseudo attribute. (specifiticy)
  • javascript: scroll to element.
  • javascript: forEach on Array-like object.

Hints

Thank you experts in internet!

HTML

<ul>
  <li>1.</li>
  <li>2.</li>
  <li>3.</li>
  <li>4.</li>
  <li>5.</li>
  <li>6.</li>
</ul>
<div id="btn" onclick="javascript:openbtn();">open...</div>

javascript

    function toggle_show(i) {
        // type: (HTMLElement)
        if (i.classList.contains("show")) {
            i.classList.remove("show");
        } else {
            i.classList.add("show");
        }
    }
    
    function openbtn(ev) {
        var i = document.getElementById("btn");
        toggle_show(i);
        var seq = document.getElementsByTagName("li");
        Array.forEach(seq, toggle_show);
        i.scrollIntoView(false);
    }

CSS

      #btn {
        position: relative;
        border: 3px solid #000;
        border-radius: 1em;
        text-align: center;
        transition: all 2s ease 0s;
        top: -3em;
      }
      #btn.show {
        top: 0em;
      }
      li:nth-child(n+3):nth-child(-n+5) {
        opacity: 0.2;
      }
      li:nth-child(n+5) {
        opacity: 0.0;
        display: none;
      }
      li.show:nth-child(n+3):not(:disabled) {
        display: block;
        opacity: 1.0;
        animation: show 1s linear 0s;
      }

      @keyframes show {
          from {opacity: 0;}
          to {opacity: 1;}
      }
<html>
<head>
<style><!--
#btn {
position: relative;
border: 3px solid #000;
border-radius: 1em;
text-align: center;
transition: all 2s ease 0s;
top: -3em;
}
#btn.show {
top: 0em;
}
li:nth-child(n+3):nth-child(-n+5) {
opacity: 0.2;
}
li:nth-child(n+5) {
opacity: 0.0;
display: none;
}
li.show:nth-child(n+3):not(:disabled) {
display: block;
opacity: 1.0;
animation: show 1s linear 0s;
}
@keyframes show {
from {opacity: 0;}
to {opacity: 1;}
}
--></style>
</head>
<body>
<ol>
<li>1.</li>
<li>2.</li>
<li>3.</li>
<li>4.</li>
<li>5.</li>
<li>1.</li>
<li>2.</li>
<li>3.</li>
<li>4.</li>
<li>5.</li>
</ol>
<div id="btn" onclick="javascript:openbtn();">open...</div>
<script><!--
function toggle_show(i) {
// type: (HTMLElement)
var text;
if (i.classList.contains("show")) {
i.classList.remove("show");
text = "open...";
} else {
i.classList.add("show");
text = "close";
}
document.getElementById("btn").innerText = text;
}
function openbtn(ev) {
var i = document.getElementById("btn");
toggle_show(i);
var seq = document.querySelectorAll("li");
// Array.forEach(seq, toggle_show); // not work on Chrome.
Array.prototype.forEach.call(seq, toggle_show);
i.scrollIntoView(false);
}
--></script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment