简体   繁体   English

为什么我不能让汉堡菜单正常工作?

[英]Why I can't get hamburger menu to work properly?

I need some help here.我需要一些帮助。 I'm trying to make a hamburger menu for my website but for some reason which I can't figure out, it doesn't work.我正在尝试为我的网站制作汉堡菜单,但由于某种我无法弄清楚的原因,它不起作用。 Here's the HTML, CSS and JavaScript code which I've written in codepen.io: https://codepen.io/TheUnrealisticProgrammer/pen/QvjvVW这是我在 codepen.io 中编写的 HTML、CSS 和 JavaScript 代码: https ://codepen.io/TheUnrealisticProgrammer/pen/QvjvVW

Here's the JavaScript code:这是 JavaScript 代码:

$(document).ready(function(){
   $('Menu').click(function(){
     $(this).toggleClass('Trigger');
   });
   });  

From the code, the first span bar should animate by rotating 135 degrees after I click on it but it's not.从代码中,第一个跨度栏应该在我点击它后旋转 135 度来设置动画,但事实并非如此。

Use Jquery and JQuery UI before using.使用前使用 Jquery 和 JQuery UI。 And $('Menu') Menu is not a tag but it is ID of div as per your Code Pen.$('Menu') Menu 不是标签,而是根据您的 Code Pen 的 div ID。 It has to be $('#Menu') .它必须是$('#Menu')

Please do these steps.请执行这些步骤。

  1. Go to your code pen转到您的代码笔
  2. Click on the setting icon available in the JS section.单击 JS 部分中可用的设置图标。
  3. Add jquery reference ( https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.0.min.js ) by clicking on the add another resource button placed at the right bottom of the dialog box.通过单击位于对话框右下角的添加另一个资源按钮添加 jquery 引用 ( https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.0.min.js )。
  4. In your javascript, you are using $('Menu').在您的 javascript 中,您使用的是 $('Menu')。 which is actually searching for the tag called Menu.这实际上是在搜索名为 Menu 的标签。 Change it to $('#Menu').将其更改为 $('#Menu')。 As you want to target to the ID.由于您要定位到 ID。
  5. It will work perfectly fine.它会工作得很好。
  1. your css style is broken,你的css样式坏了,
  2. you don't have jQuery (add through setting)你没有 jQuery(通过设置添加)
  3. $('Menu') this is jQuery as other said, you are selecting id so should be $('#Menu') $('Menu')正如其他人所说,这是 jQuery,您正在选择 id 所以应该是$('#Menu')

check this link for working example, fixed jQuery, js and CSS:检查此链接以获取工作示例,修复了 jQuery、js 和 CSS:

https://codepen.io/hdl881127/pen/wdKZVj https://codepen.io/hdl881127/pen/wdKZVj

fixed css:固定CSS:

#Menu{
  position:relative;
  margin-top:20px;
  margin-left:20px;
  display:block;
  height:50px;
  cursor: pointer;
  width:50px;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  -webkit-transition: .5s ease-in-out;
  -moz-transition: .5s ease-in-out;
  -o-transition: .5s ease-in-out;
  transition: .5s ease-in-out;
}

#Menu span{
  position:absolute;
  background:orange;
  display:block;
  height: 6px;
  width: 100%;
  border-radius:50px;  
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  -webkit-transition: .25s ease-in-out;
  -moz-transition: .25s ease-in-out;
  -o-transition: .25s ease-in-out;
  transition: .25s ease-in-out;
}
#Menu span:nth-child(1){
  top:0px;
}
#Menu span:nth-child(2){
  top:18px;
}
#Menu span:nth-child(3){
  top:36px;
}

#Menu.Trigger span:nth-child(1){
  top: 18px;
  -webkit-transform: rotate(135deg);
  -moz-transform: rotate(135deg);
  -o-transform: rotate(135deg);
  transform: rotate(135deg);
}

#Menu.Trigger span:nth-child(2){
  top: 18px;
  -webkit-transform: rotate(225deg);
  -moz-transform: rotate(225deg);
  -o-transform: rotate(225deg);
  transform: rotate(225deg);
}

#Menu.Trigger span:nth-child(3){
  top: 18px;
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

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

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