<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
.pull-left {
float: left;
}
.clearfix:after {
content: '';
display: block;
float: none;
clear: both;
}
.menu a {
display: block;
width: 179px;
height: 48px;
background: url('img/btn.png');
line-height: 48px;
text-align: center;
font-weight: bold;
color: #cfdfb5;
text-decoration: none;
}
.menu a:hover {
background: url('img/btn_over.png');
}
.sub {
position: absolute;
z-index: 1000;
max-height: 0;
overflow: hidden;
transition: max-height 180ms ease-out;
}
.menu-item {
position: relative;
}
</style>
</head>
<body>
<ul class="menu clearfix">
<li class="menu-item pull-left">
<a href="#">Frontend</a>
<ul class="sub">
<li><a href="#">HTML+CSS</a></li>
<li><a href="#">Javascript</a></li>
<li><a href="#">jQuery</a></li>
</ul>
</li>
<li class="menu-item pull-left">
<a href="#">Backend</a>
<ul class="sub">
<li><a href="#">PHP</a></li>
<li><a href="#">JSP</a></li>
<li><a href="#">Node.js</a></li>
</ul>
</li>
<li class="menu-item pull-left">
<a href="#">Mobile</a>
<ul class="sub">
<li><a href="#">iOS</a></li>
<li><a href="#">Android</a></li>
<li><a href="#">Hybrid</a></li>
</ul>
</li>
</ul>
<h1>Hello World</h1>
<script>
const menuItem = document.querySelectorAll(".menu-item");
for (let menu of menuItem) {
menu.addEventListener('mouseover', (e) => {
const current = e.currentTarget;
const sub = current.querySelector('.sub');
sub.style.maxHeight = sub.scrollHeight + "px"
});
menu.addEventListener('mouseout', (e) => {
const current = e.currentTarget;
const sub = current.querySelector(".sub");
sub.style.maxHeight = null;
});
}
</script>
</body>
</html>