简体   繁体   English

为什么单击时菜单图标不触发?

[英]Why is the menu icon not triggering when clicked on?

I have a very basic mobile nav set up to build on the functionality for future projects.我有一个非常基本的移动导航设置来构建未来项目的功能。 This looks like it should be working to me as it's a very basic toggle of the mobile nav using translateX.这看起来应该对我有用,因为它是使用 translateX 的移动导航的一个非常基本的切换。

Can anyone see an error somewhere that is preventing this from working?任何人都可以在某处看到阻止此操作的错误吗? Once I have this basic functionality I want to build on it.一旦我有了这个基本功能,我就想在它的基础上进行构建。 This is driving me crazy, Thank you for any input.这让我发疯,感谢您的任何意见。

const mobileNav = document.querySelector(".mobile-nav");
const menuIcon = document.querySelector(".bars");

menuIcon.addEventListener("click", toggleMenu);

function toggleMenu() {
  if (mobileNav.classList.contains(".mobile-nav-visible")) {
    mobileNav.classList.remove(".mobile-nav-visible");
    menuIcon.style.display = "block";
  } else {
    mobileNav.classList.add(".mobile-nav-visible");
  }
}

Here are 2 other ways you can do this这里有另外 2 种方法可以做到这一点

You have to include the "left: 0" property inside your ".mobile-nav-visible" class so it will center the nav.您必须在“.mobile-nav-visible”class 中包含“left: 0”属性,以便导航居中。 You used "transform:X(100%)" to it so it forces the nav to display outside the screen (100% to the right).您对它使用了“transform:X(100%)”,因此它强制导航显示在屏幕外(100% 向右)。

OR或者

Manipulate the display behavior through the "transform" property.通过“transform”属性操纵显示行为。 Like this像这样

.mobile-nav {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    -webkit-transform: translateX(285%);
    -moz-transform: translateX(285%);
    transform: translateX(285%); // force the nav to display outside the screen
    height: 100vh;
    width: 50%;
    background: #e6e6e6;
    transition: transform 0.3s ease-in-out;
}

.mobile-nav-visible {
    -webkit-transform: translateX(100%);
    -moz-transform: translateX(100%);
    transform: translateX(100%);
}

contains ( and all classList methods ) does not work with css selectors, but with the class name directly. contains和所有classList方法)不适用于 css 选择器,但直接使用 class 名称。 So remove the .所以删除. from the start of the class names.从 class 名称开始。

So use所以使用

function toggleMenu() {
  if (mobileNav.classList.contains("mobile-nav-visible")) {
    mobileNav.classList.remove("mobile-nav-visible");
    menuIcon.style.display = "block";
  } else {
    mobileNav.classList.add("mobile-nav-visible");
  }
}

In the if conditional where you are removing the class, set the display of your movileNav to none and then set it back to block in the else condition.在要删除 class 的 if 条件下,将movileNav 的显示设置为 none,然后将其设置回在 else 条件下阻止。

It will make your menu appear when clicking on the hamburger icon on phone but you will need to write separate css to close the menu as the button gets overlapped by the menu.单击手机上的汉堡包图标时,它将显示您的菜单,但您需要编写单独的 css 以关闭菜单,因为按钮与菜单重叠。

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

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