Issue
I have a sidebar on my page but I can't figure out a good way to make an overlay over the main content when it is open without JavaScript. Is it possible? And if not how would I go about doing it with JavaScript?
The easiest way to make a sidebar seems to be with an input checkbox like below, but I don't know if that constrains me in regards to doing what I want. I'm thinking maybe it would be easier using only JavaScript?
I apologize in advance if this is a dumb question but I don't know how to fix it and would really appreciate some help.
Here's a fiddle with my code:
https://jsfiddle.net/putyj6rv/
And my HTML because SO so desperately wants some code:
<section class='navbar'>
<div class='navbar-container'>
<div id="sidebarContainer" class=''>
<input type="checkbox" aria-label='Toggle navigation' class="openSideMenu" id="openSideMenu">
<label for="openSideMenu" class="sideIconToggle">
<div class="spinner diagonal part-1"></div>
<div class="spinner horizontal"></div>
<div class="spinner diagonal part-2"></div>
</label>
<div id="sideMenu">
<ul class="sideMenuInner">
<li class="active" onclick="hideSidebar()"><a href="#" data-toggle="tab">Item1</a></li>
<li onclick="hideSidebar()"><a href="#" data-toggle="tab">Item2</a></li>
<li onclick="hideSidebar()"><a href="#" data-toggle="tab">Item3</a></li>
</ul>
</div>
</div>
</div>
</section>
<div class='main'>
How can I get an overlay to appear / fade in/out over this area when the sidebar is open?
</div>
Solution
Here is a simple CSS hamburger menu for you. The functionality of this CSS hamburger menu relies on shifting the opacity and the location of information utilizing CSS transitions and transforms.
I also added an div labelled overlay which hides the main content with a blur effect when the checkbox is clicked.
I also linked a list of 16 other hamburger menu designs you could also utilize.
body {
margin: 0;
padding: 0;
background: #232323;
color: #cdcdcd;
font-family: Helvetica, Arial, sans-serif;
}
main {
margin: 10vw;
z-index: 0;
}
.hamburger-menu-toggle {
display: block;
position: relative;
top: 50px;
left: 50px;
z-index: 3;
user-select: none;
}
.hamburger-menu-toggle a {
text-decoration: none;
color: #232323;
transition: color 0.3s ease;
}
.hamburger-menu-toggle a:hover {
color: tomato;
}
.hamburger-menu-toggle input {
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0;
z-index: 4;
-webkit-touch-callout: none;
}
.hamburger-menu {
position: absolute;
width: 300px;
margin: -100px 0 0 -50px;
padding: 50px;
padding-top: 125px;
background: #ededed;
list-style-type: none;
transform-origin: 0% 0%;
transform: translate(-100%, 0);
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0);
z-index: 2;
}
.hamburger-menu li {
padding: 10px 0;
font-size: 22px;
}
.hamburger-menu-toggle input:checked~.hamburger-menu {
transform: none;
}
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
backdrop-filter: blur(8px);
z-index: 1;
transition: opacity 0.5s;
opacity: 0;
}
.hamburger-menu-toggle input:checked~.overlay {
opacity: 1;
}
.hamburger-menu-toggle span {
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: #cdcdcd;
border-radius: 3px;
z-index: 3;
transform-origin: 4px 0px;
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.33s ease;
}
.hamburger-menu-toggle span:first-child {
transform-origin: 0% 0%;
}
.hamburger-menu-toggle span:nth-last-child(2) {
transform-origin: 0% 100%;
}
.hamburger-menu-toggle input:checked~span {
opacity: 1;
transform: rotate(45deg) translate(-2px, -1px);
background: #232323;
}
.hamburger-menu-toggle input:checked~span:nth-last-child(3) {
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}
.hamburger-menu-toggle input:checked~span:nth-last-child(4) {
transform: rotate(-45deg) translate(-6px, 2px);
}
<body>
<nav role="navigation">
<div class="hamburger-menu-toggle">
<input type="checkbox" />
<span></span>
<span></span>
<span></span>
<ul class="hamburger-menu">
<a href="#">
<li>Item 1</li>
</a>
<a href="#">
<li>Item 2</li>
</a>
<a href="#">
<li>Item 3</li>
</a>
</ul>
<div class="overlay"></div>
</div>
</nav>
<main>
<p>How can I get an overlay to appear / fade in/out over this area when the sidebar is open?
<p>
</main>
</body>
Answered By - Nick Friesen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.