Event Listner 연습문제
✨ 실습1. test001.html 을 만들고 마우스를 올렸을 때 리스트가 보이고 마우스가 벗어나면 숨기는 이벤트리스너를 만들어보자.
test001.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
<link rel="stylesheet" href="main.css">
<script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>
</head>
<body>
<nav class="navbar ">
<div class="container-fluid">
<a class="navbar-brand" href="#">Offcanvas navbar</a>
<button class="navbar-toggler" type="button">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</nav>
<ul class="list-group" id="list1">
<li class="list-group-item">An item</li>
<li class="list-group-item">A second item</li>
<li class="list-group-item">A third item</li>
<li class="list-group-item">A fourth item</li>
<li class="list-group-item">And a fifth one</li>
</ul>
<script>
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
</body>
</html>
✨ 실습2. 위 문제가 너무 쉬운 사람만 마우스가 올라가고 해당 메뉴바의 자식에 마우스가 올라갔을때는 안사라지고 완전히 바깥으로 같을때에만 사라지게 해보자
<div id="wrap">
<div id="header">
<h1>
<a href="#">logo</a>
</h1>
<ul id="gnb" class="">
<li>
<a href="#">menu1</a>
<div class="snb">
<ul>
<li>menu1-1</li>
<li>menu1-2</li>
<li>menu1-3</li>
<li>menu1-4</li>
</ul>
</div>
</li>
<li>
<a href="#">menu2</a>
<div class="snb">
<ul>
<li>menu1-1</li>
<li>menu1-2</li>
<li>menu1-3</li>
<li>menu1-4</li>
</ul>
</div>
</li>
<li>
<a href="#">menu3</a>
<div class="snb">
<ul>
<li>menu1-1</li>
<li>menu1-2</li>
<li>menu1-3</li>
<li>menu1-4</li>
</ul>
</div>
</li>
<li>
<a href="#">menu4</a>
<div class="snb">
<ul>
<li>menu1-1</li>
<li>menu1-2</li>
<li>menu1-3</li>
<li>menu1-4</li>
</ul>
</div>
</li>
</ul>
<ul id="util">
<li><a href="#">login</a></li>
<li><a href="#">join</a></li>
</ul>
</div>
</div>
*{
margin: 0;
padding: 0;
}
ul,li{
list-style: none;
}
a{
text-decoration: none;
}
#wrap{
width: 100%;
}
#header{
position:fixed;
width: 100%;
left: 0;
top: 0;
padding: 0 30px;
box-sizing: border-box;
border-bottom: 1px solid #e1e1e1;
display:flex;
justify-content: space-between;
}
#header h1{
padding: 20px 0;
}
#header > #gnb {
width: 1200px;
display: flex;
justify-content: space-evenly;
align-items: center;
}
#header > #gnb::before {
content: "";
width: 100%;
position:absolute;
top: 85px;
left: 0;
height: 180px;
background: #fff;
border-bottom: 1px solid #e1e1e1;
box-shadow: 0px 10px 10px 0px rgba(48,49,51,6%);
display: block;
}
#header > #gnb::before {
display: none;
}
#header > #gnb.on::before {
display: block;
}
#header > #gnb > li .snb {
position:absolute;
top: 100px;
display:none;
}
#header > #gnb.on > li .snb {
display: block;
}
#header > #gnb > li > .snb > ul > li + li {
margin-top:20px;
}
스크립트 작성
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="navbar navbar-default" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li><a href="#">Separated link</a></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li><a href="#">Separated link</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div>
</div>
<script>
$('.navbar .dropdown').hover(function() {
$(this).find('.dropdown-menu').first().stop(true, true).slideDown(150);
}, function() {
$(this).find('.dropdown-menu').first().stop(true, true).slideUp(105)
});
</script>
</body>
</html>
이 사이트에서 간단한 코드들 작성하고 확인하기 쉽다.
Last updated