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

In-depth analysis of the Linux kernel macro container_of

1. As mentioned above I saw this macro when I was...

vue $set implements assignment of values ​​to array collection objects

Vue $set array collection object assignment In th...

The latest virtual machine VMware 14 installation tutorial

First, I will give you the VMware 14 activation c...

Introduction to HTML_PowerNode Java Academy

What is HTML? HTML is a language used to describe...

Vue3+el-table realizes row and column conversion

Table of contents Row-Column Conversion Analyze t...

JavaScript to implement login slider verification

This article example shares the specific code of ...

How to create a virtual environment using virtualenv under Windows (two ways)

Operating system: windowns10_x64 Python version: ...

Parsing Linux source code epoll

Table of contents 1. Introduction 2. Simple epoll...

Reasons why MySQL queries are slow

Table of contents 1. Where is the slowness? 2. Ha...

XHTML 1.0 Reference

Arrange by functionNN : Indicates which earlier ve...

Nginx Location directive URI matching rules detailed summary

1. Introduction The location instruction is the c...

Native JS to achieve cool paging effect

This article uses an example to share with you a ...

Detailed Analysis of the Selection of MySQL Common Index and Unique Index

Suppose a user management system where each perso...