I'm trying to create a hamburger menu in which after you click it, it turns into a close ("X") button. So I put together this HTML for the hamburger menu
<div id="mobile-nav">
<div class="menuContainer">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<nav>
<a href='#'>Vote</a>
<a href='#'>Search</a>
<a href='#'>Contact</a>
</nav>
</div>
and these styles
.menuContainer {
display: inline-block;
cursor: pointer;
}
.bar1, .bar2, .bar3 {
width: 25px;
height: 4px;
background-color: #333;
margin: 5px 0;
transition: 0.4s;
}
.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-9px, 6px);
transform: rotate(-45deg) translate(-9px, 6px);
}
.change .bar2 {opacity: 0;}
.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px);
transform: rotate(45deg) translate(-8px, -8px);
}
However, when I click on the button, the "X" seems cut off on the right side -- https://jsfiddle.net/8jvrrcqq/3/ . What can I do to make the X even on both the left and right sides?
You just needed to tweak your translate values a bit. Also consider that you can use translateY
and rotateZ
since you only care about these directions for the animation.
$('.close-btn').click(function() {
$('.responsive-menu').removeClass('expand')
$('.menu-btn').removeClass('btn-none')
})
$('.menuContainer').click(function() {
$(this).toggleClass('change');
$('nav').toggleClass('nav-open');
});
#mobile-nav {
display: block;
}
.menuContainer {
display: inline-block;
cursor: pointer;
}
.bar1,
.bar2,
.bar3 {
width: 25px;
height: 4px;
background-color: #333;
margin: 5px 0;
transition: 0.4s;
}
.change .bar1 {
transform: translateY(9px) rotateZ(45deg);
}
.change .bar2 {
opacity: 0;
}
.change .bar3 {
transform: translateY(-9px) rotateZ(-45deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mobile-nav">
<div class="menuContainer">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
</div>