JSFiddle - React, Tailwind, and code Playground
HTML
<header>
<div class="inner-header">
<nav>
<ul>
<li class="parent">
<a href="#" title="bus">bus</a>
<div class="locations-wrapper">
<ul class="sub-menu">
<li><a href="#" title="bus-station">station</a></li>
<li><a href="#" title="bus-stop">stop</a></li>
</ul>
</div>
</li>
<li class="parent">
<a href="#" title="train">train</a>
<div class="locations-wrapper">
<ul class="sub-menu">
<li><a href="#" title="train-station">station</a></li>
<li><a href="#" title="train-stop">stop</a></li>
</ul>
</div>
</li>
</ul>
</nav>
</div>
<div class="boxes"></div>
</header>
CSS
* {
margin: 0;
padding: 0;
}
header {
height: 36px;
background-color: grey;
position: relative;
}
nav {
width: 100%
}
nav ul > li {
width: 300px;
float: left;
text-align: center;
list-style-type: none;
padding: 9px 0;
z-index: 12;
}
nav ul li a {
display: block;
color: white;
}
.locations-wrapper {
display: none;
position: relative;
background-color: blue;
height: 300px;
width: 300px;
z-index: 12;
top: 9px;
}
.sub-menu {
z-index: 12;
height: 300px;
}
.sub-menu li {
display: block;
float: none;
text-align: left;
padding: 0;
width: 100%;
background-color: grey;
opacity: .5;
z-index: 12;
}
.sub-menu li a {
display: block;
background-color: yellow;
opacity: .5;
padding: 5px 0;
z-index: 12;
}
.boxes > div {
position: absolute;
top: 36px;
height: 300px;
width: 100%;
z-index: 10;
display: none;
}
.open { z-index: 11;}
.closed { display: none;}
.bus { background-color: green;}
.bus-station { background-color: orange;}
.bus-stop { background-color: purple;}
.train { background-color: red;}
.train-station { background-color: yellow;}
.train-stop { background-color: brown;}
JavaScript
var parentList = $('.parent');
// declare variables
var currentChildTitle;
var currentTitle;
var banner;
// setup append div function
function dropBanner(currentTitle){
// create the banner variable dom node
banner = $('<div class="' + currentTitle + '"/></div>');
// add it to the dom
$('.boxes').append(banner);
// animate it
$('.' + currentTitle).stop().slideDown(300);
}
// setup a function to limit the number of divs appended
function chop(){
if ($('.boxes div').length > 15) {
$('.boxes div').eq(0).remove();
}
}
// listen for mouseover the parent list items
parentList.on('mouseover', function(e) {
if (!($(this).find('.locations-wrapper').is(':visible'))) {
$(this).find('.locations-wrapper').stop().slideDown(300);
};
// grab the current list item title
currentTitle = $(this).find('a').attr('title');
// call dropBanner passing the current list item title
dropBanner(currentTitle);
chop();
});
// listen for mouseleave
parentList.on('mouseleave', function() {
$(this).find('.locations-wrapper').delay(300).slideUp(300);
$('.boxes div').delay(300).slideUp(300);
});
// listen for mouseover the submenu list items
$('.sub-menu').on('mouseover', 'li', function(e){
// grab the current list item title
currentTitle = $(this).find('a').attr('title');
// call dropBanner passing the current list item title
dropBanner(currentTitle);
chop();
// stop the bubbling up effect to parent list items
e.stopPropagation();
});