Menu #8
- 12+
- 36+
- 29+
- 23+
- 9+
- 36+
- 95+
- 24+
- 9+
- 3+
- 37+
- 4.09 KB
- HTML, CSS, JS
- Menu
- dropdown, vertical
- MIT License
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" />
<ul id="accordion" class="accordion">
<li>
<div class="link"><i class="fa fa-database"></i>Web Design<i class="fa fa-chevron-down"></i></div>
<ul class="submenu">
<li><a href="#">Photoshop</a></li>
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
</ul>
</li>
<li>
<div class="link"><i class="fa fa-code"></i>Coding<i class="fa fa-chevron-down"></i></div>
<ul class="submenu">
<li><a href="#">Javascript</a></li>
<li><a href="#">jQuery</a></li>
<li><a href="#">Ruby</a></li>
</ul>
</li>
<li>
<div class="link"><i class="fa fa-mobile"></i>Devices<i class="fa fa-chevron-down"></i></div>
<ul class="submenu">
<li><a href="#">Tablet</a></li>
<li><a href="#">Mobile</a></li>
<li><a href="#">Desktop</a></li>
</ul>
</li>
<li>
<div class="link"><i class="fa fa-globe"></i>Global<i class="fa fa-chevron-down"></i></div>
<ul class="submenu">
<li><a href="#">Google</a></li>
<li><a href="#">Bing</a></li>
<li><a href="#">Yahoo</a></li>
</ul>
</li>
</ul>
body {
font-family: "Open Sans", Arial, Helvetica, Sans-serif, Verdana, Tahoma;
}
ul {
list-style-type: none;
}
* {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
a {
color: #b63b4d;
text-decoration: none;
}
.accordion {
width: 100%;
max-width: 360px;
margin: 30px auto 20px;
background: #fff;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
.accordion .link {
cursor: pointer;
display: block;
padding: 15px 15px 15px 42px;
color: #4d4d4d;
font-size: 14px;
font-weight: 700;
border-bottom: 1px solid #ccc;
position: relative;
-webkit-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
transition: all 0.4s ease;
}
.accordion li:last-child .link {
border-bottom: 0;
}
.accordion li i {
position: absolute;
top: 16px;
left: 12px;
font-size: 18px;
color: #595959;
-webkit-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
transition: all 0.4s ease;
}
.accordion li i.fa-chevron-down {
right: 12px;
left: auto;
font-size: 16px;
}
.accordion li.open .link {
color: #b63b4d;
}
.accordion li.open i {
color: #b63b4d;
}
.accordion li.open i.fa-chevron-down {
-webkit-transform: rotate(180deg);
-o-transform: rotate(180deg);
-moz-transform: rotate(180deg);
transform: rotate(180deg);
}
.submenu {
display: none;
background: #444359;
font-size: 14px;
}
.submenu li {
border-bottom: 1px solid #4b4a5e;
}
.submenu a {
display: block;
text-decoration: none;
color: #d9d9d9;
padding: 12px;
padding-left: 42px;
-webkit-transition: all 0.25s ease;
-o-transition: all 0.25s ease;
-moz-transition: all 0.25s ease;
transition: all 0.25s ease;
}
.submenu a:hover {
background: #b63b4d;
color: #fff;
}
$(function () {
var Accordion = function (el, multiple) {
this.el = el || {};
this.multiple = multiple || false;
var links = this.el.find(".link");
links.on("click", { el: this.el, multiple: this.multiple }, this.dropdown);
};
Accordion.prototype.dropdown = function (e) {
var $el = e.data.el;
($this = $(this)), ($next = $this.next());
$next.slideToggle();
$this.parent().toggleClass("open");
if (!e.data.multiple) {
$el.find(".submenu").not($next).slideUp().parent().removeClass("open");
}
};
var accordion = new Accordion($("#accordion"), false);
});
The code snippet we’re looking at is a wonderful blend of HTML, CSS, and JavaScript, intricately woven together to create a stylish and functional dropdown menu. This type of menu, often referred to as an accordion for its ability to expand and collapse in a smooth motion, brings an elegant solution to the challenge of navigating through various categories or options without cluttering the webpage.
Starting with the HTML, it lays the foundation of our accordion-style dropdown menu through a structured list. Each item within this list is designed to be clickable, revealing a hidden submenu that slides out with more options. This vertical menu structure is ideal for efficiently organizing content while maintaining an aesthetically pleasing design.
The CSS part is where the magic happens in terms of styling. It not only gives life to our menu with colors, fonts, and spacing but also handles the hidden/revealed state of each submenu. Specific styles are applied to ensure that the menu is not only functional but also visually appealing, with smooth transitions that enhance the user experience. The use of CSS transitions for the dropdown effect ensures that the expansion and collapse of the menu are smooth and natural, contributing to a polished and professional look.
Finally, the JavaScript code powers the interactive behavior of the dropdown menu. It listens for clicks on the menu items, triggering the expansion or collapse of the associated submenu. The script cleverly allows for either a single submenu to be open at a time or multiple submenus, depending on how it’s configured. This flexibility makes the dropdown menu versatile, suitable for a wide range of web design needs.
Together, these three components create a dropdown menu that is not only visually appealing but also enhances the user experience with its smooth, interactive functionality. It’s a testament to how HTML, CSS, and JavaScript can be combined to create web elements that are both beautiful and practical.