To achieve the desired behavior—where closing the Authorization or Registration forms returns the user to the primary menu without completely hiding everything—you need to adjust the JavaScript logic. Specifically, after a form is closed, the primary menu should be reactivated instead of remaining closed.
Here's how you can modify your existing code to implement this functionality:
Update the JavaScript to Reopen the Menu When a Form Is Closed:
Remove the closeMenu() call from the openForm function to prevent the menu from closing when a form is opened.
Modify the event listeners for the close buttons to not only hide the form but also ensure the primary menu is active.
Ensure the Hamburger Icon Reflects the Menu State Properly:
When the menu is active, the hamburger icon should show an "X" to indicate that clicking it will close the menu.
When the menu is hidden, the hamburger icon should revert to its original state.
Here is the revised code with the necessary changes:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Auth Forms with Hidden Hamburger</title>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
}
.hamburger {
position: fixed;
top: 20px;
right: 20px;
width: 30px;
height: 30px;
cursor: pointer;
z-index: 100;
transition: opacity 0.3s ease;
}
.hamburger span {
display: block;
width: 100%;
height: 2px;
background-color: #333;
margin: 6px 0;
transition: 0.4s;
}
.menu, .auth-form, .reg-form {
position: fixed;
top: 0;
right: -300px;
width: 300px;
height: 100vh;
background-color: #f1f1f1;
transition: 0.4s;
padding-top: 60px;
box-sizing: border-box;
z-index: 90;
}
.menu.active, .auth-form.active, .reg-form.active {
right: 0;
}
.menu ul {
list-style-type: none;
padding: 0;
margin: 0;
}
.menu ul li {
padding: 10px 20px;
}
.menu ul li a, .menu ul li button {
color: #333;
text-decoration: none;
background: none;
border: none;
font-size: 16px;
cursor: pointer;
padding: 0;
}
.hamburger.active span:nth-child(1) {
transform: rotate(-45deg) translate(-5px, 6px);
}
.hamburger.active span:nth-child(2) {
opacity: 0;
}
.hamburger.active span:nth-child(3) {
transform: rotate(45deg) translate(-5px, -6px);
}
.close {
position: absolute;
top: 20px;
right: 20px;
font-size: 24px;
cursor: pointer;
}
form {
display: flex;
flex-direction: column;
padding: 20px;
}
input, button[type="submit"] {
margin-bottom: 10px;
padding: 5px;
}
.hidden {
opacity: 0;
pointer-events: none;
}
</style>
</head>
<body>
<div class="hamburger">
<span></span>
<span></span>
<span></span>
</div>
<nav class="menu">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
<li><button id="auth-btn">Authorization</button></li>
<li><button id="reg-btn">Registration</button></li>
</ul>
</nav>
<div class="auth-form">
<span class="close">×</span>
<form>
<h2>Authorization</h2>
<input type="email" placeholder="Email" required>
<input type="password" placeholder="Password" required>
<button type="submit">Log In</button>
</form>
</div>
<div class="reg-form">
<span class="close">×</span>
<form>
<h2>Registration</h2>
<input type="text" placeholder="Name" required>
<input type="email" placeholder="Email" required>
<input type="password" placeholder="Password" required>
<input type="password" placeholder="Confirm Password" required>
<button type="submit">Register</button>
</form>
</div>
<script>
const hamburger = document.querySelector('.hamburger');
const menu = document.querySelector('.menu');
const authForm = document.querySelector('.auth-form');
const regForm = document.querySelector('.reg-form');
const authBtn = document.getElementById('auth-btn');
const regBtn = document.getElementById('reg-btn');
const closeBtns = document.querySelectorAll('.close');
function closeAllForms() {
authForm.classList.remove('active');
regForm.classList.remove('active');
// No need to hide hamburger here
}
function openForm(form) {
// Removed closeMenu() to keep the menu open when a form is opened
closeAllForms();
form.classList.add('active');
hamburger.classList.add('hidden');
}
hamburger.addEventListener('click', () => {
hamburger.classList.toggle('active');
menu.classList.toggle('active');
closeAllForms();
});
authBtn.addEventListener('click', () => openForm(authForm));
regBtn.addEventListener('click', () => openForm(regForm));
closeBtns.forEach(btn => {
btn.addEventListener('click', (e) => {
const form = e.target.closest('.auth-form, .reg-form');
form.classList.remove('active');
hamburger.classList.remove('hidden');
// Reactivate the hamburger icon to reflect the menu state
// Check if the menu should be open based on hamburger state
// If the menu was previously open before opening the form, re-open it
if (hamburger.classList.contains('active')) {
menu.classList.add('active');
} else {
menu.classList.remove('active');
}
});
});
// Optional: Close the menu when clicking outside of it or on a menu link
document.addEventListener('click', function(event) {
const isClickInsideMenu = menu.contains(event.target) || hamburger.contains(event.target);
if (!isClickInsideMenu) {
menu.classList.remove('active');
hamburger.classList.remove('active');
closeAllForms();
}
});
</script>
</body>
</html>
Explanation of Changes:
Removed closeMenu() from openForm Function:
This allows the primary menu to remain open when a form is opened, ensuring that the menu items are still accessible or can be reactivated easily.
Modified Close Button Event Listeners:
After closing a form, the code now checks if the hamburger icon has the active class. If it does, it reactivates the menu by adding the active class to the .menu element. This ensures that the primary menu is visible again after a form is closed.
Ensured Hamburger Icon Visibility:
The hamburger icon is unhidden when a form is closed, allowing users to interact with the menu again.
Optional Enhancement – Click Outside to Close Menu:
Added an event listener to the entire document to close the menu if the user clicks outside of it. This improves the user experience by allowing users to close the menu without having to click the hamburger icon again.
Additional Recommendations:
Accessibility Considerations:
Ensure that the interactive elements are keyboard accessible.
Add aria attributes to improve screen reader support.
Smooth Transitions:
You may enhance user experience by adding smoother transitions or animations when opening and closing menus and forms.
Responsive Design:
Test the layout on various screen sizes to ensure that the menu and forms are responsive and function correctly on all devices.
By implementing these changes, closing the Authorization or Registration forms will now return the user to the primary menu, maintaining an intuitive and user-friendly navigation experience.