HTML+Sass implements HambergurMenu (hamburger menu)

HTML+Sass implements HambergurMenu (hamburger menu)

A few days ago, I watched a video of a foreign guy using HTML+CSS to implement HamburgerMenu. Recently, I have been watching Sass, so I will use Sass to implement it.

The final effect is as follows:

File structure in VS Code (easy sass is used to compile scss files):

Page structure (index.html):

_config.scss:

/*Set color and max-width*/
$primary-color: rgba(13,110,139,.75);
$overlay-color: rgba(24,39,51,.85);
$max-width: 980px;

/*Set the text color. If the background color is light, set it to black, otherwise set it to white*/
@function set-text-color($color){
  @if(lightness($color)>70){
    @return #333;
  }@else{
    @return #fff;
  }
}

/*Mixer, set background color and text color*/
@mixin set-background($color){
  background-color: $color;
  color: set-text-color($color);
}

style.scss:

@import '_config';
*{
  margin: 0;
  padding: 0;
}

.container{
  max-width: $max-width;
  margin: 0 auto;
}

/*Set the background color for showcase, add a background image using pseudo-class, and set the z-index of the background image to -1 (so that the image will be displayed inside);
Then in order to make the text appear in the middle, flex layout is used*/
.showcase{
  position: relative;
  height: 100vh;
  background-color: $primary-color;
  &:before{
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: url('../img/pexels-photo-533923.jpeg') no-repeat center center / cover;
    z-index: -1;
  }
  &-inner{
    display: flex;
    height: 100%;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    color: #fff;
    font-weight: 100;
    h1{
      font-size: 4rem;
      padding: 1.2rem 0;
    }
    p{
      white-space: pre-wrap;
      font-size: 1.6rem;
      padding: 0.85rem 0;
    }
    .btn{
      padding: .65rem 1rem;
        /*Use the mixer to set the background color and text color*/
      @include set-background(lighten($primary-color,30%));
      border: none;
      border: 1px solid $primary-color;
      border-radius: 5px;
      text-decoration: none;
      outline: none;
      transition: all .2s ease .1s;
        /*When the button is hovered, use CSS3 scale to set a zoom effect*/
      &:hover{
        @include set-background(lighten($overlay-color,30%));
        border-color: lighten($overlay-color, 25%);
        transform: scale(.98);
      }
    }
  }
}

/*Principle: Change the style by clicking on the checkbox (set the checkbox transparency to 0 and the z-index to a higher level). When you click, a menu will appear. Click again and the menu will disappear*/
/*Set menu-wrap to fixed so that the showcase will fill the entire screen; also set the opacity of the checkbox to 0;
Also note that you need to set the z-index of the checkbox to 2, because the z-index of the hamburger is set to 1, otherwise the click will not work*/
.menu-wrap{
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1;
  .toggle{
    position: absolute;
    left: 0;
    top: 0;
    width: 50px;
    height: 50px;
    opacity: 0;
    z-index: 2;
    cursor: pointer;
      /*When the checkbox is checked, set the rotation effect of the div and pseudo class in the hamburger*/
    &:checked ~ .hamburger>div{
      transform: rotate(135deg);
        /*The pseudo class is actually rotated 225 degrees, so top needs to be set to 0, otherwise the length of the ❌ will be inconsistent*/
      &:before,&:after{
        transform: rotate(90deg);
        top: 0;
      }
    }

      /*When the checkbox is selected and hovered, a rotation effect will also be set*/
    &:checked:hover ~ .hamburger>div{
      transform: rotate(235deg);
    }
    &:checked ~ .menu{
      visibility: visible;
      >div{
        transform: scale(1);
        >div{
          opacity: 1;
        }
      }
    }
  }
    
  .hamburger{
    position: absolute;
    left: 0;
    top: 0;
    width: 60px;
    height: 60px;
    padding: 1rem;
    background-color: $primary-color;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    z-index: 1;
      /*The div itself generates a horizontal line in the middle, then sets the positioning to relative, and then sets its pseudo-class to absolute.
      Offset relative to div*/
    >div{
      position: relative;
      left: 0;
      top: 0;
      width: 100%;
      height: 2px;
      background-color: #fff;
      transition: all .7s ease;
        /*Use pseudo-class to generate the first and third horizontal lines*/
      &:before,
      &:after{
        content: '';
        position: absolute;
        left: 0;
        top: -10px;
        width: 100%;
        height: 2px;
        background-color: inherit;
      }
      &:after{
        top: 10px;
      }
    }
  }

    /*Set the style of the selected menu*/
    /*Set the menu to fixed, with a width and height of 100%, and then set the display to flex, otherwise the menu will not appear in the middle*/
  .menu{
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    visibility: hidden; /*Set the menu to invisible, and then set it to visible when the checkbox is selected*/
    transition: all .75s ease;
    >div{
      @include set-background($overlay-color);
      width: 200vw;
      height: 200vh;
      overflow: hidden;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      text-align: center;
      transform: scale(0);
      transition: all .4s ease;
      >div{
        max-width: 90vw;
        max-height: 90vh;
        opacity: 0;
        transition: all .4s ease;
        >ul>li{
          list-style: none;
          font-size: 2rem;
          padding: .85rem 0;
          text-transform:uppercase;
          transform: skew(-5deg,-5deg) rotate(5deg);/*Set text tilt*/
          a{
            color: inherit;
            text-decoration: none;
            transition: color .4s ease;
          }
        }
      }
    }
  }
}

This is the end of this article about HTML+Sass implementation of HambergurMenu (hamburger menu). For more relevant HTML+Sass implementation of HambergurMenu content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

<<:  Detailed examples of using JavaScript event delegation (proxy)

>>:  Example of exporting and importing Docker containers

Recommend

How to start multiple MySQL databases on a Linux host

Today, let’s talk about how to start four MySQL d...

A very detailed explanation of the Linux DHCP service

Table of contents 1. DHCP Service (Dynamic Host C...

How MySQL Select Statement is Executed

How is the MySQL Select statement executed? I rec...

What to do if you forget your mysql password

Forgot your MySQL password twice? At first I did ...

Web front-end performance optimization

Web front-end optimization best practices: conten...

Implementation of element input box automatically getting focus

When making a form in a recent project, I need to...

Creation, constraints and deletion of foreign keys in MySQL

Preface After MySQL version 3.23.44, InnoDB engin...

How to set up a deployment project under Linux system

1. Modify the firewall settings and open the corr...

Steps to create a CentOS container through Docker

Table of contents Preface Create a bridge network...

9 Tips for MySQL Database Optimization

Table of contents 1. Choose the most appropriate ...

Usage and description of HTML tag tbody

The tbody element should be used in conjunction wi...

Javascript design pattern prototype mode details

Table of contents 1. Prototype mode Example 1 Exa...

Vue2.x responsiveness simple explanation and examples

1. Review Vue responsive usage​ Vue responsivenes...

Example of how to upload a Docker image to a private repository

The image can be easily pushed directly to the Do...

JavaScript uses canvas to draw coordinates and lines

This article shares the specific code of using ca...