I created a responsive side menu that appears when I resize the browser window. My "problem" is the following: when I resize the browser window, this side menu appears and disappears quickly, without clicking on the appropriate button... In other case, everything works perfectly, it's only this problem that bothers me.
Does anyone could tell me where this issue comes from ?
var btn = document.querySelector(".toggle-btn");
var nav = document.querySelector(".nav");
btn.addEventListener("click", function () {
btn.classList.toggle("nav-open");
nav.classList.toggle("nav-open");
});
@media screen and (max-width: 1300px) {
html,
body {
margin: 0px;
padding: 0px;
}
.toggle-btn {
height: 30px;
width: 30px;
position: relative;
float: right;
top: 10px;
right: 20px;
cursor: pointer;
z-index: 1000;
}
.toggle-btn span {
height: 3px;
background-color: #2c3e50;
width: 100%;
position: absolute;
top: 20px;
left: 0;
transition: .4s;
}
.toggle-btn span:before {
content: '';
height: 3px;
background-color: #2c3e50;
width: 100%;
position: absolute;
top: -10px;
left: 0;
transition: .4s;
}
.toggle-btn span:after {
content: '';
height: 3px;
background-color: #2c3e50;
width: 100%;
position: absolute;
top: 10px;
left: 0;
transition: .4s;
}
.menu {
height: 1000px;
background-color: #eeebec;
width: 250px;
position: absolute;
right: 0px;
}
.menu a {
color: text-align: center;
display: block;
padding-top: 30px;
}
.nav {
margin-right: -250px;
transition-duration: 0.4s;
}
.nav-open {
margin-right: 0px;
transition-duration: 0.4s;
}
.toggle-btn.nav-open span {
background: rgba(0, 0, 0, 0);
}
.toggle-btn.nav-open span:before {
top: 0;
transform: rotate(45deg);
}
.toggle-btn.nav-open span:after {
top: 0;
transform: rotate(-45deg);
}
}
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Toggle Button</title>
<link rel="stylesheet" href="btn.css">
</head>
<body>
<div class="toggle-btn">
<span></span>
</div>
<div class="menu nav">
Blah
Blah
Blah
</div>
<script src="btn.js"></script>
</body>
</html>
transform: rotate(45deg);
}
.toggle-btn.nav-open span:after {
top: 0;
transform: rotate(-45deg);
}
}