简体   繁体   English

如何使JQuery切换不移动元素?

[英]How do I make JQuery toggle not move elements around?

I'm trying to make a responsive website menu which is openable with a burger (3 lines). 我正在尝试制作一个响应式网站菜单,该菜单可以用一个汉堡包打开(3行)。

I've got the menu to open but it keeps moving the burger up and down in order to make way for it. 我已经打开菜单,但是为了让它腾出空间,它一直在上下移动汉堡。

How do I make it so you click the burger and it stays in the SAME PLACE whilst opening the menu and playing its animation? 我如何制作它,以便您在打开菜单并播放动画时单击汉堡并将其保留在SAME PLACE中?

<div class="burger" id="burger" onclick="openBurger(this)">
    <div class="bar1"></div>
    <div class="bar2"></div>
    <div class="bar3"></div>
</center>

JS JS

function openBurger(x) {
    x.classList.toggle("change");
}

$("#burger").click(function() { 
$("#nav").toggle();
});

Burger 汉堡包

在此处输入图片说明

As you can see, the burger bumps down to make space for the menu. 如您所见,汉堡撞下来,为菜单留出空间。 在此处输入图片说明

Thanks. 谢谢。

 function myFunction(x) { x.classList.toggle("change"); } 
 .container { display: inline-block; cursor: pointer; } .bar1, .bar2, .bar3 { width: 35px; height: 5px; background-color: #333; margin: 6px 0; transition: 0.4s; } .change .bar1 { -webkit-transform: rotate(-45deg) translate(-9px, 6px); transform: rotate(-45deg) translate(-9px, 6px); } .change .bar2 {opacity: 0;} .change .bar3 { -webkit-transform: rotate(45deg) translate(-8px, -8px); transform: rotate(45deg) translate(-8px, -8px); } 
 <div class="container" onclick="myFunction(this)"> <div class="bar1"></div> <div class="bar2"></div> <div class="bar3"></div> </div> 

Your hamburger menu should live independently from your nav list. 您的汉堡菜单应独立于导航列表而生活。 With this example, you can include it before the list, and use jQuery to toggle the state of the navigation. 在此示例中,您可以将其包括在列表之前,并使用jQuery切换导航状态。

 $("#burger").click(function() { var state = $("#nav").attr("class"); $("#nav").attr("class", state == "show" ? "hide" : "show"); }); 
 #burger { font-size: 32px; text-decoration: none; } #nav { display: none; } #nav.show { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a id="burger" href="#">☰</a> <ul id="nav"> <li><a class="bar1" href="#">Home</a></li> <li><a class="bar2" href="#">Work</a></li> <li><a class="bar3" href="#">Contact</a></li> </ul> 

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

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