Home >Web Front-end >CSS Tutorial >jQuery and CSS3 stunning hamburger deformation animation effects
Brief Tutorial
This is a cool hamburger deformation animation special effect made using jQuery and CSS3. This special effect attaches button events through jQuery and creates animation effects through CSS3 transform and animation.
How to use
HTML structure
The HTML structure of the hamburger deformation animation effect is as follows:
<div class='container'> <div class='burger'> <div class='burger__line-top'></div> <div class='burger__line-mid'></div> <div class='burger__menu'> <p>MENU</p> </div> </div> </div>
CSS style
Hamburger menu button The basic style is as follows:
.burger { position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; width: 71px; height: 71px; cursor: pointer; } .burger__line-top { width: 100%; height: 8px; border-radius: 5px; background-color: #fff; box-shadow: 0 0 1px 0 #fff; } .burger__line-mid { margin-top: 17px; width: 100%; height: 8px; border-radius: 5px; background-color: #fff; box-shadow: 0 0 1px 0 #fff; } .burger__menu { margin-top: 10px; } .burger__menu p { text-align: center; font-size: 20px; font-family: 'Open Sans', sans-serif; font-weight: 900; color: #fff; text-shadow: 0 0 1px #fff; letter-spacing: 3px; }
Six animations are used in the style: activeTop, activeMid, activeMenu, reverseTop, reverseMid and reverseMenu. Used respectively to deform and return to the initial state of the hamburger button.
JavaScript
This special effect uses jQuery code to add and remove corresponding classes for corresponding elements, and bind mouse click events to the hamburger button.
'use strict'; $(document).ready(function () { var $burger = $('.burger'), $topLine = $('.burger__line-top'), $midLine = $('.burger__line-mid'), $menuLine = $('.burger__menu'), anim = false; var changeClasses = { addActive: function addActive() { for (var i = 0; i <= 2; i++) { $burger.children().eq(i).removeClass('reverseLine' + (i + 1)).addClass('activeLine' + (i + 1)); } }, addReverse: function addReverse() { for (var i = 0; i <= 2; i++) { $burger.children().eq(i).removeClass('activeLine' + (i + 1)).addClass('reverseLine' + (i + 1)); } } }; var timeouts = { initial: function initial(child, Y, rot, scale) { $burger.children().eq(child).css('transform', 'translateY(' + Y + 'px) rotate(' + rot + 'deg) scale(' + scale + ',1)'); }, afterActive: function afterActive() { var _this = this; // ES6 setTimeout(function () { _this.initial(0, 12, 45, 1.40); _this.initial(1, -12, -45, 1.40); _this.initial(2, 35, 0, 1); $burger.children().eq(2).css('opacity', '0'); anim = true; }, 1300); }, beforeReverse: function beforeReverse() { var _this2 = this; setTimeout(function () { for (var i = 0; i <= 2; i++) { _this2.initial(i, 0, 0, 1); } $burger.children().eq(2).css('opacity', '1'); anim = false; }, 1300); } }; $burger.on('click', function () { if (!anim) { changeClasses.addActive(); timeouts.afterActive(); } else if (anim) { changeClasses.addReverse(); timeouts.beforeReverse(); } }); });
The above is the content of the stunning hamburger deformation animation special effects of jQuery and CSS3. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!