Created
April 5, 2017 16:43
-
-
Save imliam/eb895626bfd0aae29ffe51b6ff108492 to your computer and use it in GitHub Desktop.
Bootstrap 4 - Animated Hamburger Icon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Additional spin to the animation. | |
.hamburger { | |
display: inline-block; | |
width: 1.5em; | |
height: 1.5em; | |
padding-top: 0.3em; | |
padding-bottom: 0.3em; | |
vertical-align: middle; | |
content: ""; | |
background: center center / 100% 100% no-repeat; | |
transform: rotateY(0); | |
transition: all .4s cubic-bezier(0.23, 1, 0.32, 1); | |
&__patty { | |
width: 100%; | |
height: 2px; | |
margin: 0 0 4px 0; | |
background: white; | |
transition: all .4s cubic-bezier(0.23, 1, 0.32, 1); | |
&:last-child { | |
margin-bottom: 0; | |
} | |
} | |
// transform burger into 'x' | |
&--close { | |
transform: rotate(180deg); | |
.hamburger__patty { | |
&:nth-child(1) { | |
transform: rotate(45deg) translate(4px, 4px); | |
} | |
&:nth-child(2) { | |
opacity: 0; | |
} | |
&:nth-child(3) { | |
transform: rotate(-45deg) translate(5px, -5px); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
|-------------------------------------------------------------------------- | |
| Bootstrap 4 - Animated Hamburger Icon | |
|-------------------------------------------------------------------------- | |
| | |
| Adds a quick animation to the hamburger icon when expanding or | |
| collapsing a navbar. | |
| | |
--> | |
<nav class="navbar navbar-toggleable-md navbar-inverse bg-inverse"> | |
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> | |
<div class="hamburger d-flex flex-column justify-content-around text-center"> | |
<div class="hamburger__patty"></div> | |
<div class="hamburger__patty"></div> | |
<div class="hamburger__patty"></div> | |
</div> | |
</button> | |
<a class="navbar-brand" href="#">Navbar</a> | |
<div class="collapse navbar-collapse" id="navbarNav"> | |
<ul class="navbar-nav"> | |
<li class="nav-item active"> | |
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link" href="#">Features</a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link" href="#">Pricing</a> | |
</li> | |
</ul> | |
</div> | |
</nav> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function () { | |
$($('.hamburger').parent().attr('data-target')).on('hide.bs.collapse', function () { | |
$(this).parent().find('.hamburger').removeClass('hamburger--close'); | |
}); | |
$($('.hamburger').parent().attr('data-target')).on('show.bs.collapse', function () { | |
$(this).parent().find('.hamburger').addClass('hamburger--close'); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Standard animation, moves two of the patties to the same position for a smooth transition. | |
.hamburger { | |
display: inline-block; | |
width: 1.5em; | |
height: 1.5em; | |
padding-top: 0.5em; | |
padding-bottom: 0.5em; | |
vertical-align: middle; | |
content: ""; | |
background: center center / 100% 100% no-repeat; | |
position: relative; | |
&__patty { | |
display: block; | |
width: 100%; | |
height: 2px; | |
background: white; | |
position: absolute; | |
transition: all 200ms; | |
&:nth-child(1) { | |
top: 0.3em; | |
} | |
&:nth-child(2) { | |
top: 50%; | |
transform: translateY(-50%); | |
} | |
&:nth-child(3) { | |
bottom: 0.3em; | |
} | |
} | |
&--close { | |
.hamburger__patty { | |
&:nth-child(1), | |
&:nth-child(2) { | |
top: 0; | |
transform: translateY(0.65em) rotate(45deg); | |
} | |
&:nth-child(3) { | |
transform: translateY(-0.5em) rotate(-45deg); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment