Animated - Hamburger Icon (spans)

In this example below you will see how to do a Animated - Hamburger Icon (spans) with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by matchboxhero, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright matchboxhero ©
  • HTML
  • CSS
  • JavaScript
    <div class="container">
	<a href="#" class="menu-button [ js-menu-button ]">
	    <div class="menu-icon">
		   <span></span>
		   <span></span>
		   <span></span>
		   <span></span>
	    </div>
	</a>
</div>

/*Downloaded from https://www.codeseek.co/matchboxhero/animated-hamburger-icon-spans-XexMRo */
    .container {
	display: flex;
	align-items: center;
	justify-content: center;
	background-color: #ededed;
	min-height: 100vh;
}

/////
//
// Menu Icon
//
/////

$menu-icon-width: 3em;
$menu-icon-height: $menu-icon-width * 0.75;
$menu-icon-color: #363636;
$menu-icon-color--active: #D00251;

.menu-icon {
	position: relative;
    width: $menu-icon-width;
	height: $menu-icon-height;
	display: inline-block;
}

.menu-icon span {
    position: absolute;
    height: $menu-icon-width / 16;
    width: 100%;
    background-color: $menu-icon-color;
    transition: all 250ms ease-in-out;
    transform: rotate(0deg);
	box-shadow: 0px 1px 1px rgba(255,255,255,0.6);
}

.menu-icon span:nth-child(1) {
    top: $menu-icon-width / 32;
}
	
.menu-icon span:nth-child(2),
.menu-icon span:nth-child(3) {
    top: $menu-icon-width / 3.2;
}

.menu-icon span:nth-child(4) {
    top: $menu-icon-width * 0.625;
}

.menu-button:hover span,
.menu-button:focus span,
.menu-button:active span {
    background: $menu-icon-color--active;
}

.menu-icon.is-active span:nth-child(1),
.menu-icon.is-active span:nth-child(4) {
    opacity: 0; 
}

.menu-icon.is-active span:nth-child(2) {
    transform: rotate(45deg);
}

.menu-icon.is-active span:nth-child(3) {
	top: $menu-icon-width / 3.2;
	transform: rotate(-45deg);
}


/*Downloaded from https://www.codeseek.co/matchboxhero/animated-hamburger-icon-spans-XexMRo */
    function updateMenuButton() {
	$('.js-menu-button').find('.menu-icon').toggleClass('is-active');
}

$(document).ready(function() {

	$('.js-menu-button').click(function(e){

		e.preventDefault();
		updateMenuButton();

	});

});

Comments