简体   繁体   English

如何在点击时旋转汉堡菜单图标

[英]How to rotate hamburger menu icons on click

I have a problem with my hamburger menu, especially with the icons.我的汉堡菜单有问题,尤其是图标。 My HTML looks like this:我的 HTML 看起来像这样:

 <input type="checkbox" id="check"/>
 <header class="IndexHeader">
   <nav class="navigation">
       <label class="Hamburger" for="check">&#9776;</label>
       <label class="schliessen" for="check">&#9932;</label>
       <ul class="IndexNavliste">
           a list...
       </ul>  
    </nav>            
 </header>

I want my .schliessen label to rotate 180 degrees when I click on the .Hamburger label, so that it is like an animation effect.当我单击.Hamburger label 时,我希望我的.schliessen label 旋转 180 度,这样它就像 animation 效果一样。 I tried it like this with jQuery:我用 jQuery 像这样尝试过:

$(".Hamburger").click(function(){
   $(".schliessen").css("transform","rotate(180deg)");
});

That didn't really work for me.那对我来说真的不起作用。 Also, I think I need a transition in it so that I really can see it when I click the label.另外,我认为我需要在其中进行过渡,以便在单击 label 时真正可以看到它。 I also tried to do it in the CSS directly so when my checkbox is checked.我还尝试直接在 CSS 中执行此操作,因此当我的复选框被选中时。 That works but I couldn't see the animation and my hover effect didn't work anymore after that...那行得通,但我看不到 animation 和我的 hover 效果在那之后不再起作用了......

.Hamburger{
  display: block;
  transition: 500ms;
}
.schliessen{
  transition: 500ms;
}
.schliessen:hover{
  color: black;
}
#check:checked + .IndexHeader .navigation .Hamburger{
  display: none;
}
#check:checked + .IndexHeader .navigation .schliessen{
  display: block;
  transform: rotate(180deg);
}

The issue with transition and transform:rotate("180deg") is happening because transform never gets a new value from first click onwards on Hamburger. transitiontransform:rotate("180deg")正在发生,因为transform从第一次点击汉堡包开始就不会获得新值。 To overcome that I have added and removed classes on clicks.为了克服这个问题,我添加和删除了点击类。

Have also removed un-necessary CSS and added what is required for this problem:还删除了不必要的 CSS 并添加了此问题所需的内容:

Here is the working code:这是工作代码:

codepen.io/emmeiWhite/pen/XWjBXRY?editors=1111 codepen.io/emmeiWhite/pen/XWjBXRY?editors=1111

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

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