[前端] HTML+Sass实现HambergurMenu(汉堡包式菜单)

2119 0
黑夜隐士 2022-10-19 15:54:53 | 显示全部楼层 |阅读模式
前几天看了国外一个大佬用HTML+CSS实现HamburgerMenu的视频,然后最近在看Sass,所以用Sass来实现一下。
最终效果图如下:


在VS Code中的文件结构(编译scss文件用的是easy sass):


页面结构(index.html):
_config.scss:
  1. /*设置颜色及max-width*/
  2. $primary-color: rgba(13,110,139,.75);
  3. $overlay-color: rgba(24,39,51,.85);
  4. $max-width: 980px;
  5. /*设置文本颜色,如果背景色浅,则设置为黑色,否则设置为白色*/
  6. @function set-text-color($color){
  7.   @if(lightness($color)>70){
  8.     @return #333;
  9.   }@else{
  10.     @return #fff;
  11.   }
  12. }
  13. /*混合器,设置背景色及文本颜色*/
  14. @mixin set-background($color){
  15.   background-color: $color;
  16.   color: set-text-color($color);
  17. }
复制代码
style.scss:
  1. @import '_config';
  2. *{
  3.   margin: 0;
  4.   padding: 0;
  5. }
  6. .container{
  7.   max-width: $max-width;
  8.   margin: 0 auto;
  9. }
  10. /*给showcase设置背景颜色,利用伪类再添加一个背景图,同时将背景图的z-index设置为-1(这样图片会显示在里面);
  11. 然后为了让文字显示在中间,所以使用flex布局*/
  12. .showcase{
  13.   position: relative;
  14.   height: 100vh;
  15.   background-color: $primary-color;
  16.   &:before{
  17.     content: '';
  18.     position: absolute;
  19.     left: 0;
  20.     top: 0;
  21.     width: 100%;
  22.     height: 100%;
  23.     background: url('../img/pexels-photo-533923.jpeg') no-repeat center center / cover;
  24.     z-index: -1;
  25.   }
  26.   &-inner{
  27.     display: flex;
  28.     height: 100%;
  29.     flex-direction: column;
  30.     justify-content: center;
  31.     align-items: center;
  32.     text-align: center;
  33.     color: #fff;
  34.     font-weight: 100;
  35.     h1{
  36.       font-size: 4rem;
  37.       padding: 1.2rem 0;
  38.     }
  39.     p{
  40.       white-space: pre-wrap;
  41.       font-size: 1.6rem;
  42.       padding: 0.85rem 0;
  43.     }
  44.     .btn{
  45.       padding: .65rem 1rem;
  46.         /*使用混合器设置背景色及文本颜色*/
  47.       @include set-background(lighten($primary-color,30%));
  48.       border: none;
  49.       border: 1px solid $primary-color;
  50.       border-radius: 5px;
  51.       text-decoration: none;
  52.       outline: none;
  53.       transition: all .2s ease .1s;
  54.         /*按钮hover的时候利用css3的scale设置一个缩放效果*/
  55.       &:hover{
  56.         @include set-background(lighten($overlay-color,30%));
  57.         border-color: lighten($overlay-color, 25%);
  58.         transform: scale(.98);
  59.       }
  60.     }
  61.   }
  62. }
  63. /*原理:通过checkbox的点中与否来进行样式的变化(将checkbox透明度设置为0,z-index设置更高),单击时,会出现菜单,再次点击,菜单消失*/
  64. /*给menu-wrap设置fixed,这样showcase就会占满整个屏幕;同时将checkbox的opacity设置为0;
  65. 另外注意,需要将checkbox的z-index设置为2,因为hamburger的z-index设置为1,不然会点击不起作用
  66. */
  67. .menu-wrap{
  68.   position: fixed;
  69.   left: 0;
  70.   top: 0;
  71.   z-index: 1;
  72.   .toggle{
  73.     position: absolute;
  74.     left: 0;
  75.     top: 0;
  76.     width: 50px;
  77.     height: 50px;
  78.     opacity: 0;
  79.     z-index: 2;
  80.     cursor: pointer;
  81.       /*当checkbox为checked时,设置hamburger里面的div及伪类的旋转效果*/
  82.     &:checked ~ .hamburger>div{
  83.       transform: rotate(135deg);
  84.         /*伪类实际上旋转了225度,需要将top设置为0,不然形成的长短不一致*/
  85.       &:before,&:after{
  86.         transform: rotate(90deg);
  87.         top: 0;
  88.       }
  89.     }
  90.       /*当checkbox选中时再hover,也会设置一个旋转效果*/
  91.     &:checked:hover ~ .hamburger>div{
  92.       transform: rotate(235deg);
  93.     }
  94.     &:checked ~ .menu{
  95.       visibility: visible;
  96.       >div{
  97.         transform: scale(1);
  98.         >div{
  99.           opacity: 1;
  100.         }
  101.       }
  102.     }
  103.   }
  104.   .hamburger{
  105.     position: absolute;
  106.     left: 0;
  107.     top: 0;
  108.     width: 60px;
  109.     height: 60px;
  110.     padding: 1rem;
  111.     background-color: $primary-color;
  112.     box-sizing: border-box;
  113.     display: flex;
  114.     flex-direction: column;
  115.     justify-content: center;
  116.     align-items: center;
  117.     text-align: center;
  118.     z-index: 1;
  119.       /*div自身生成中间的横线,然后设置定位为relative,后面再将其伪类设置为absolute,
  120.       相对于div进行偏移*/
  121.     >div{
  122.       position: relative;
  123.       left: 0;
  124.       top: 0;
  125.       width: 100%;
  126.       height: 2px;
  127.       background-color: #fff;
  128.       transition: all .7s ease;
  129.         /*利用伪类生成第一条和第三条横线*/
  130.       &:before,
  131.       &:after{
  132.         content: '';
  133.         position: absolute;
  134.         left: 0;
  135.         top: -10px;
  136.         width: 100%;
  137.         height: 2px;
  138.         background-color: inherit;
  139.       }
  140.       &:after{
  141.         top: 10px;
  142.       }
  143.     }
  144.   }
  145.     /*设置选中后的菜单的样式*/
  146.     /*将menu设置为fixed,同时宽高为100%,然后设置display为flex,否则菜单不会出现在中间*/
  147.   .menu{
  148.     position: fixed;
  149.     left: 0;
  150.     top: 0;
  151.     width: 100%;
  152.     height: 100%;
  153.     overflow: hidden;
  154.     display: flex;
  155.     justify-content: center;
  156.     align-items: center;
  157.     visibility: hidden;    /*将菜单设置为不可见,然后在checkbox选中时设置为可见*/
  158.     transition: all .75s ease;
  159.     >div{
  160.       @include set-background($overlay-color);
  161.       width: 200vw;
  162.       height: 200vh;
  163.       overflow: hidden;
  164.       border-radius: 50%;
  165.       display: flex;
  166.       justify-content: center;
  167.       align-items: center;
  168.       text-align: center;
  169.       transform: scale(0);
  170.       transition: all .4s ease;
  171.       >div{
  172.         max-width: 90vw;
  173.         max-height: 90vh;
  174.         opacity: 0;
  175.         transition: all .4s ease;
  176.         >ul>li{
  177.           list-style: none;
  178.           font-size: 2rem;
  179.           padding: .85rem 0;
  180.           text-transform: uppercase;
  181.           transform: skew(-5deg,-5deg) rotate(5deg);/*设置文字倾斜*/
  182.           a{
  183.             color: inherit;
  184.             text-decoration: none;
  185.             transition: color .4s ease;
  186.           }
  187.         }
  188.       }
  189.     }
  190.   }
  191. }
复制代码
到此这篇关于HTML+Sass实现HambergurMenu(汉堡包式菜单)的文章就介绍到这了,更多相关HTML+Sass实现HambergurMenu内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章,希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行