Animation Transition for a mobile menu toggle. Three horizontal lines transform into a cross and back again. The top and bottom lines fall into the center, transforming into one line. The line(s) rotate clockwise, spawning a line at 45deg and stop at 135 deg, forming a cross. Demo: http://jsfiddle.net/SYYr5/84/
Annotations:
- the animation uses four additional elements inside the a tag. This is not semantic, but we can at least use elements without a semantic meaning, like div and span
- using a div as a second container, we are able to apply a padding to our a tag
- We may need to use vendor prefixes :/
The HTML-Snippet:
<a id="toggle-menu">
<div>
<span class="top"></span>
<span class="middle"></span>
<span class="bottom"></span>
</div>
</a>
The SCSS-Snippet:
#toggle-menu {
display: block;
width: 15px;
height: 15px;
padding: 20px;
cursor: pointer;
div {
width: 15px;
height: 15px;
position: relative;
}
span {
display: block;
width: 15px;
height: 3px;
background: black;
position: absolute;
transition: transform 0.5s ease-in-out, top 0.25s ease-in-out 0.5s, opacity 0.25s ease-in-out 0.5s;
transform-origin: center;
&.top {
top: 0px;
}
&.middle {
top: 6px;
}
&.bottom {
top: 12px;
}
}
&.menu-is-active {
span {
transition: transform 0.5s ease-in-out 0.3s, top 0.25s ease-in-out, opacity 0.25s ease-in-out;
&.top {
top: 6px;
transform: rotate(135deg);
}
&.middle {
opacity: 0;
transform: rotate(135deg);
}
&.bottom {
top: 6px;
transform: rotate(225deg);
}
}
}
}
When using my jQuery-Plugin adjust-at, the following javascript will provide interactivity:
$(document).ready(function() {
var nav = $('header nav');
$('#toggle-menu').adjustAt({
below850: function(element) {
nav.hide();
element.click(function() {
element.toggleClass('menu-is-active');
nav.slideToggle(800);
});
},
above850: function(element) {
nav.show();
element.off('click');
}
});
});
Why use animations when you can just use transforms?
Example