简体   繁体   English

如何更改基础汉堡包图标以在单击时关闭图标(即:移动菜单打开)

[英]How to change foundation hamburger icon to close icon on click (i.e: mobile menu open)

In foundation menu , the drop-down menu icon is in the button as follows-在基础菜单中,下拉菜单图标在按钮中,如下所示-

<button class="menu-icon dark" type="button" data-toggle=""></button>

普通菜单图标

.menu-icon.dark::after {
    position: absolute;
    top: 0;
    left: 0;
    display: block;
    width: 100%;
    height: 2px;
    background: #0a0a0a;
    -webkit-box-shadow: 0 7px 0 #0a0a0a, 0 14px 0 #0a0a0a;
    box-shadow: 0 7px 0 #0a0a0a, 0 14px 0 #0a0a0a;
    content: '';
}

How can i make it so that when the mobile menu is open the icon changes to this-我怎样才能做到这一点,当移动菜单打开时,图标会变成这个 -

在此处输入图片说明

And vice verse反之亦然

Codepen link to topbar Codepen 链接到顶栏

You'll need to add custom code to achieve this.您需要添加自定义代码来实现这一点。 The solution is that you add an event listener that listens for the user to click on the button.解决方案是添加一个事件侦听器来侦听用户单击按钮。 Once clicked, add an active class and style the active class as needed in the stylesheet.单击后,添加一个活动类并根据需要在样式表中设置活动类的样式。

The below code should help.下面的代码应该会有所帮助。

Add the following code in your style file:在您的样式文件中添加以下代码:

//SCSS for Active class

.menu-icon.dark{
   &:after{
     transition: all 0.3s ease;
  }
     &:before{
     transition: all 0.3s ease;
  }
}
.menu-icon.dark.active{
  transition: all 0.3s ease;

  &:after{
    transition: all 0.3s ease;
    background: #0a0a0a;
    box-shadow: none;
    transform: rotate(45deg);
    top: 12px;
    &:hover{
      opacity: 0.9;
    }

  }
   &:before{
     transition: all 0.3s ease;
    position: absolute;
    top: 12px;
    left: 0;
    display: block;
    width: 100%;
    height: 2px;
    background: #0a0a0a;
    box-shadow: none;
    transform: rotate(-44deg);
    content: "";
    &:hover{
      opacity: 0.9;
    }
  }
}

In the javascript file, add the following code:在 javascript 文件中,添加以下代码:

//Adds active class to the menu icon

$('.menu-icon').on('click', function(){

  $(this).toggleClass("active");

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM