#
24.메뉴
In
#
1. 요소의 크기 얻기
CDN
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<div class="box">box</div>
jQuery
console.log('height:' + $('.box').height()); //
console.log('innerHeight:' + $('.box').innerHeight());
console.log('outerHeight:' + $('.box').outerHeight());
VanillaJS
console.log('height:' + document.querySelector(".box").clientHeight;
console.log('offsetHeight:' + document.querySelector(".box").offsetHeight;
console.log('outerHeight:' + document.querySelector(".box").scrollHeight;
#
투뎁스 구조작성
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<header>
<nav>
<ul class="df">
<li>
1st menu
<ul>
<li>2nd menu</li>
<li>2nd menu</li>
<li>2nd menu</li>
<li>2nd menu</li>
<li>2nd menu</li>
</ul>
</li>
<li>
1st menu
<ul>
<li>2nd menu menu</li>
<li>2nd menu</li>
</ul>
</li>
<li>
1st menu
<ul>
<li>2nd menu</li>
<li>2nd menu</li>
</ul>
</li>
<li>
1st menu
<ul>
<li>2nd menu</li>
<li>2nd menu</li>
</ul>
</li>
</ul>
</nav>
</header>
<script src="http://code.jquery.com/jquery-1.3.1.min.js"></script>
<!-- <script src="js/script.js"></script> -->
<script src="js/script2.js"></script>
</body>
</html>
* {
margin: 0;
padding: 0;
}
ul li {
list-style: none;
}
.df {
display: flex;
}
body {
background: green;
overflow-y: scroll;
}
header {
margin-top: 20px;
background: linear-gradient(white 50px, rgba(255, 255, 255, 0.5) 50px, rgba(255, 255, 255, 0.5));
height: 50px;
overflow: hidden;
transition: all 1s;
}
header.active {
margin-top: 20px;
background: linear-gradient(black 50px, rgba(0, 0, 0, 0.5) 50px, rgba(0, 0, 0, 0.5));
height: 50px;
overflow: hidden;
}
header.active a,
header.active li {
color: #fff;
}
nav {
width: 960px;
margin: 0 auto;
}
nav > ul > li {
line-height: 50px;
padding: 0 40px;
position: relative;
}
nav > ul > li:hover {
background-color: red;
}
nav > ul > li ul {
width: 100%;
display: block;
position: absolute;
}
nav > ul > li ul li {
white-space: nowrap;
}
jQuery
var mainmenu = $('nav > ul > li '), //원뎁스 li 할당
subMenu = mainmenu.find('ul'), //투뎁스 ul 할당
header = $('header'), //header 할당
headerHeight = header.height(), //header 높이할당
subMenuHeight = subMenu.height(), //subMenu 높이할당
totalHeaderHeight = headerHeight + subMenuHeight + 30; //totalHeaderHeight 변수에 header 높이+subMenu높이 할당
mainmenu.mouseenter(function () {
header
.addClass('active')
.stop()
.animate({ height: totalHeaderHeight + 'px' }, 300);
});
mainmenu.mouseleave(function () {
header
.removeClass('active')
.stop()
.animate({ height: headerHeight + 'px' }, 300);
});
vanillaJS
const mainmenu = document.querySelectorAll('nav > ul > li'); // 원뎁스 li 할당
const header = document.querySelector('header'); // header 할당
mainmenu.forEach((menu) => {
const subMenu = menu.querySelector('ul'); // 투뎁스 ul 할당
menu.addEventListener('mouseenter', function () {
const headerHeight = header.clientHeight; //header 높이할당
const subMenuHeight = subMenu.clientHeight; // subMenu 높이 할당
const totalHeaderHeight = headerHeight + subMenuHeight + 30; // totalHeaderHeight 변수에 header 높이+subMenu 높이 할당
header.classList.add('active');
header.style.transition = 'all 0.5s';
header.style.height = totalHeaderHeight + 'px';
});
menu.addEventListener('mouseleave', function () {
header.classList.remove('active');
header.style.transition = 'all 0.5s';
header.style.height = 'auto';
});
});