I used CSS and a little JavaScript to demonstrate how to generate a "hamburger" animation.
In a previous article, I posted my inspiration for the animation of the navigation bar menu (also known as the "hamburger icon"). As a result, I received many compliments from fans. I would like to thank you in advance.
I decided to work more on this, so I wrote a series of different animation implementations. In this article, you will see enhanced versions of 6 different "hamburger" animations.
I used a little JavaScript to trigger the animation. Before continuing to study, you can take a look at the Demo (Translator: If it cannot be opened, please click on the Demo at the bottom of the article)
Changes
The code I wrote before was not very neat. The main purpose of the changes is to make the code more tidy and orderly.
First, I replaced the meaningless div with a button element. This makes the code more readable.
Then I named the wrapper class name .hamburger. Inside .hamburger, I have another element (a span tag), and I positioned its class name as .icon
Essence
Our .icon element is wrapped to form a complete icon.
In order to make our animation smoother, the middle bar needs to be flexible enough, so the .icon element is used to act as the patty part of the hamburger.
As for the icon wrapping part, .hamburger not only acts as a wrapper, it also assists in completing the entire animation. Therefore, the upper and lower parts of the hamburger are filled by the .hamburger pseudo-class.
HTML
<button class="hamburger hamburger-cancel"> <span class="icon"></span></button>
As you can see, the button element plays the .hamburger role. Another class name (.hamburger-cancel) is used to distinguish what kind of burger it is (pan-fried chicken drumstick or Big Mac or something). We have 6 different flavors of burgers here.
JavaScript
As I said at the beginning, we use a small piece of JS code to switch, the following is my code snippet
var el = document.querySelectorAll('.hamburger');for(i=0; i<=el.length; i++) { el[i].addEventListener('click', function() { this.classList.toggle('active'); }, false);}
The above JavaScript is mainly By binding the click time, add the .active class to the corresponding element.
Code
The following CSS code sets the default state of our hamburger element. You can adjust the font-size appropriately. Of course, the maximum font-size cannot exceed our icon
In addition to structural specifications, we also use the CSS transition property to make the animation smoother.
.hamburger { font-size: 60px; display: inline-block; width: 1em; height: 1em; padding: 0; cursor: pointer; transition: transform .2s ease-in-out; vertical-align: middle; border: 0 none; background: transparent; }/** * Button height fix for Firefox */.hamburger::-moz-focus-inner { padding: 0; border: 0 none; }/** * Focus fix for Chrome */.hamburger:focus { outline: 0; }
Now, it’s time to draw our burger icon. The following definition can give us a clearer understanding of the entire process.
Obviously, all three of the icons The bars all have some common characteristics that we can extract.
.hamburger:before,.hamburger:after { content: ""; }.hamburger:before,.hamburger .icon,.hamburger:after { display: block; width: 100%; height: .2em; margin: 0 0 .2em; transition: transform .2s ease-in-out; border-radius: .05em; background: #596c7d; }/** * Styles for the active `.hamburger` icon */.hamburger.active:before,.hamburger.active .icon,.hamburger.active:after { background: #2c3e50; }
In the above code, we drew three bars in .hamburger. If we give it a little gap, we can clearly see the hamburger icon.
In order to look sleeker, we set the border-radius attribute. Since our elements have no text, we set a background color.
As expected, we have drawn a Shandong pancake. Let's move on to the next animation part.
Animation
6 different flavors of burgers, let’s analyze them one by one.
Vertical
In the simplest way, to rotate the burger, we only need to rotate it 90 degrees in the .active state, or 270 degrees, which will look cooler. a little.
/** * VERTICAL HAMBURGER */.hamburger.hamburger-vertical.active { transform: rotate(270deg); }
Rotate the bread and an 'X' shape will appear.
Of course, just spinning is not enough. While rotating, you also need to make changes on the Y axis, otherwise the distance will be a bit far.
Plus sign
/** * CLOSE/CANCEL/CROSS */.hamburger.hamburger-cancel.active .icon { transform: scale(0); }.hamburger.hamburger-cancel.active:before { transform: translateY(.4em) rotate(135deg); }.hamburger.hamburger-cancel.active:after { transform: translateY(-.4em) rotate(-135deg); }
This animation is like this:
- The patty in the middle disappears - The upper bun moves and rotates to act as a vertical bar
- The bun at the bottom moves and rotates to act as a horizontal line
These 3 actions can complete the animation from hamburger to plus
Minus sign
/** * PLUS */.hamburger.hamburger-plus.active .icon { transform: scale(0); }.hamburger.hamburger-plus.active:before { transform: translateY(.4em) rotate(90deg); }.hamburger.hamburger-plus.active:after { transform: translateY(-.4em) rotate(180deg); }
Minus sign The change is like this, the middle layer still disappears, the other two pieces move up and down, and finally merge into one piece. Then let them rotate 180 degrees in the same way, which will be very cool~
Left arrow
/** * MINUS/DASH */.hamburger.hamburger-minus.active { transform: rotate(180deg); }.hamburger.hamburger-minus.active .icon { transform: scale(0); }.hamburger.hamburger-minus.active:before { transform: translateY(.4em); }.hamburger.hamburger-minus.active:after { transform: translateY(-.4em); }
This includes moving, rotating and adjusting the upper and lower pieces of bread. Finally, add a 180-degree rotation
right arrow
/** * LEFT ARROW */.hamburger.hamburger-arrow-left.active { transform: rotate(180deg); }.hamburger.hamburger-arrow-left.active:before { width: .6em; transform: translateX(.4em) translateY(.2em) rotate(45deg); }.hamburger.hamburger-arrow-left.active .icon { border-radius: .1em .25em .25em .1em; }.hamburger.hamburger-arrow-left.active:after { width: .6em; transform: translateX(.4em) translateY(-.2em) rotate(-45deg); }
This is the mirror copy of the left arrow. You can copy the code of the left arrow and make appropriate changes.
Summary
/** * RIGHT ARROW */.hamburger.hamburger-arrow-right.active { transform: rotate(180deg); }.hamburger.hamburger-arrow-right.active:before { width: .6em; transform: translateX(0) translateY(.2em) rotate(-45deg); }.hamburger.hamburger-arrow-right.active .icon { border-radius: .25em .1em .1em .25em; }.hamburger.hamburger-arrow-right.active:after { width: .6em; transform: translateX(0) translateY(-.2em) rotate(45deg); }
I looked for similar implementations online and I found Sara’s Navicon transformicons to be cool, robust and more appealing.
I hope you enjoyed this article and feel free to share your thoughts and suggestions below. Thanks for reading.
http://helkyle.com/demos/hamburger-icons.html’ target=’__blank’>Click to view Demo
This article is based on the translation by @Rahul Arora. The entire translation contains my own understanding and meaning. If there is anything that is not translated well or is wrong, please give me some advice. If you want to reprint this translation, please indicate the English source: http://w3bits.com/animated-hamburger-icons/

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
