Table of Contents
Changes
Essence
HTML
JavaScript
Code
Animation
Vertical
Rotate the bread and an 'X' shape will appear.
This animation is like this:
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~
This includes moving, rotating and adjusting the upper and lower pieces of bread. Finally, add a 180-degree rotation
This is the mirror copy of the left arrow. You can copy the code of the left arrow and make appropriate changes.
I looked for similar implementations online and I found Sara’s Navicon transformicons to be cool, robust and more appealing.
Home Web Front-end HTML Tutorial 6 CSS hamburger animations_html/css_WEB-ITnose

6 CSS hamburger animations_html/css_WEB-ITnose

Jun 24, 2016 am 11:43 AM

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>
Copy after login

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);}
Copy after login

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; }
Copy after login

Now, it’s time to draw our burger icon. The following definition can give us a clearer understanding of the entire process.

  • Top bun: hamburger:before
  • Middle Orleans chicken leg: .icon
  • Bottom bun: .hamburger:after
  • 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; }
    Copy after login

    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); }
    Copy after login

    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); }
    Copy after login

    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); }
    Copy after login

    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); }
    Copy after login

    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); }
    Copy after login

    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); }
    Copy after login

    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/

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

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

    Hot Article

    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Nordhold: Fusion System, Explained
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1666
    14
    PHP Tutorial
    1273
    29
    C# Tutorial
    1253
    24
    HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

    The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

    The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

    The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

    The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

    The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

    HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

    The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

    HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

    HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

    HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

    HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

    HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

    HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

    From Text to Websites: The Power of HTML From Text to Websites: The Power of HTML Apr 13, 2025 am 12:07 AM

    HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

    See all articles