This time I want to share a vertical accordion menu with a dark background based on CSS3. The implementation principle is similar to the previous one, but the menu display form is slightly different. When the accordion menu is expanded, the sub-menu will have a horizontal fly-in animation. effect.
Advertisement
<ul class="menu"> <li class="list"><a href="#">List 1 </a> <ul class="items"> <li><a href="#"> Item 1-1</a></li> <li><a href="#"> Item 1-2</a></li> <li><a href="#"> Item 1-3</a></li> </ul> </li> <li class="list"><a href="#">List 2</a> <ul class="items"> <li> <a href="#" > Item 2-1 </a></li> <li> <a href="#" > Item 2-2 </a></li> <li> <a href="#" > Item 2-3 </a></li> </ul> </li> <li class="list"><a href="#">List 3</a> <ul class="items"> <li> <a href="#" > Item 3-1 </a></li> <li> <a href="#" > Item 3-2 </a></li> <li> <a href="#" > Item 3-2 </a></li> </ul> </li> <li class="list"><a href="#">List 4</a> <ul class="items"> <li> <a href="#" > Item 4-1 </a></li> <li> <a href="#" > Item 4-2 </a></li> <li class="list"><a href="#">List 4-1</a> <ul class="items"> <li> <a href="#" > Item 4-1-1</a></li> <li> <a href="#" > Item 4-1-2</a></li> <li class="list"><a href="#">List 4-2</a> <ul class="items"> <li> <a href="#" > Item 4-2-1</a></li> <li> <a href="#" > Item 4-2-2</a></li> </ul> </li> </ul> </li> </ul> </li> </ul>
body { padding: 0; margin: 0; background-color: #324252; } h1 { font-family: 'Century Gothic'; margin: 30px auto 30px auto; color: #09fbd2; width: 100%; text-align: center; } ul.menu { padding: 0; list-style: none; width: 400px; margin: 20px auto; font-family: 'Century Gothic'; box-shadow: 0px 0px 25px #00000070; clear: both; display: table; } ul.menu .list { font-size: 14px; border-bottom: 1px solid #324252; position: relative; width: 100%; box-sizing: border-box; height: 50px; vertical-align: sub; background: #3e5165; clear: both; }
var list = document.querySelectorAll('.list'); function accordion(e) { e.stopPropagation(); if (this.classList.contains('active')) { this.classList.remove('active'); } else if (this.parentElement.parentElement.classList.contains('active')) { this.classList.add('active'); } else { for (i = 0; i < list.length; i++) { list[i].classList.remove('active'); } this.classList.add('active'); } } for (i = 0; i < list.length; i++) { list[i].addEventListener('click', accordion); }
Advertisement