javascript 我想打开我的导航栏,但它似乎不会让我这样做

nfzehxib  于 2023-04-19  发布在  Java
关注(0)|答案(1)|浏览(124)

因此,我坚持试图让导航栏打开我有它,所以当它被点击它应该得到骑的隐藏链接标签,但它似乎只是不切换它,并保持ul隐藏

import React from "react";
import { NavLink } from 'react-router-dom';
import '../styles/navstyle.css'
import { MdMenu } from 'react-icons/md';
import { MdClose } from 'react-icons/md';
import { useState } from 'react';

export default function NavMenu () {
    const [ open, setOpen ] = useState(false);

    return(

        <div className="nav">
            <div className="moblie-menu-icon"
                onClick={() => setOpen(!open)} 
                role="button"
                >
                <MdMenu />
            </div>
            <ul className={ !setOpen ? 'nav-links' : 'nav-links hide-links'}>
                <div className="closeNavIcon" 
                onClick={() => setOpen(!open)} 
                role="button" 
                >
                    <MdClose />
                </div>
                <li className="link">
                    <NavLink to="/" exact>Home</NavLink>
                </li>
                <li className="link">
                    <NavLink to="/about">About</NavLink>
                </li>
                <li className="link">
                    <NavLink to="/projects">Projects</NavLink>
                </li>
                <li className="link">
                    <NavLink to="/contact">Contact</NavLink>
                </li>
            </ul>
        </div>
    );
}

如果你需要的话,这里是css

.nav {
    position: fixed;
    z-index: 100;
    top: 0;
    left: 0;
    width: 100%;
    padding: 1rem 0;
    background-color: var(--dark-bg);
}

ul {
    display: flex;
    justify-content: center;
    align-items: center;
    max-width: 1200px;
    margin: 0 auto;
    width: 90%;
}

li {
    display: block;
    border-radius: 8px;
    transition: 0.3s ease background-color;
}

li:hover {
    background-color: var(--deep-dark);
}

a {
    padding: 1rem 2rem;
    font-size: 2rem;
    color: var(--gary-1);
    outline: none;
}

.active {
    color: var(--white)
}

.moblie-menu-icon {
    position: absolute;
    right: 1rem;
    top: 1rem;
    width: 4rem;
    cursor: pointer;
    display: none;
    outline: none;
}

.closeNavIcon {
    display: none;
}

@media only screen and (max-width: 768px) {
    * {
        pointer-events: none;
    }
    .moblie-menu-icon {
        display: block;
    }

    .hide-links {
        transform: translateY(calc(-100% - var(--top)));
    }

    .nav-links {
        --top: 1rem;
        display: inline-block;
        transition: 0.3s ease transform;
        background-color: var(--deep-dark);
        padding: 2rem;
        width: 90%;
        max-width: 300px;
        border-radius: 12px;
        position: absolute;
        right: 1rem;
        top: var(--top);
        cursor: pointer;
    }

    .closeNavIcon {
        display: block;
        width: 3rem;
        margin: 0 0 0 auto;
    }

    .link {
        display: block;
        margin-bottom: 1rem;
    }

}

我尝试切换类或添加一个show-links类时,它会被点击,但似乎没有什么工作

cyvaqqii

cyvaqqii1#

问题在下面一行:

<ul className={ !setOpen ? 'nav-links' : 'nav-links hide-links'}>

使用**!open代替!setOpen**,应该可以正常工作。

相关问题