-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch7onmousedropdownmenujs.html
72 lines (64 loc) · 1.69 KB
/
ch7onmousedropdownmenujs.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html>
<head>
<title>Dropdown Menu with Animation</title>
<style>
/* Dropdown button styles */
.dropbtn {
background-color: #4CAF50;
color: rgb(167, 51, 51);
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
opacity: 0;
visibility: hidden;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
transition: opacity 0.9s ease-in-out, visibility 0.9s ease-in-out;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #ddd;}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
opacity: 1;
visibility: visible;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {background-color: #3e8e41;}
</style>
</head>
<body>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
<script>
// JavaScript for opening and closing the dropdown menu
let dropdown = document.querySelector('.dropdown');
dropdown.addEventListener('mouseover', function() {
this.querySelector('.dropdown-content').classList.add('show');
});
dropdown.addEventListener('mouseout', function() {
this.querySelector('.dropdown-content').classList.remove('show');
});
</script>
</body>
</html>