我的导航栏线在减小窗口大小时不显示出来。

huangapple go评论84阅读模式
英文:

My Nav Bar lines are not showing up when reducing the window size

问题

I had implemented a nav bar in one of my websites, and when I tried to implement the same nav bar in another website I'm creating, the hamburger lines do not show up. I can't seem to find the problem in it. The code is in react (exactly like my old implementation) and the component's code is below:

import React, { useEffect, useState } from 'react';
import { Link } from "react-router-dom";
import './Navbar.scss';

function Navbar() {
    const [click, setClick] = useState(false);
    const [button, setButton] = useState(true);

    const handleClick = () => setClick(!click);
    const closeMobileMenu = () => setClick(false);

    const showButton = () => {
        if (window.innerWidth <= 960) {
            setButton(false);
        } else {
            setButton(true);
        }
    };

    useEffect(() => {
        showButton();
    }, []);

    window.addEventListener('resize', showButton);

    return (
        <>
            <div className='header-container'>
                <p>Book a visit by calling us 9</p>
            </div>
            <nav className='navbar'>
                <div className='navbar-container'>
                    <Link to='/' className='navbar-logo' onClick={closeMobileMenu}>
                        LOGOG
                    </Link>
                    {/* Below is the hamburger for the navigation bar */}
                    <div className='menu-icon' onClick={handleClick}>
                        <i className={click ? 'fas fa-times' : 'fas fa-bars'} />
                    </div>
                    <ul className={click ? 'nav-menu active' : 'nav-menu'}>
                        <li className='nav-item'>
                            <Link to='/' className='nav-links' onClick={closeMobileMenu}>
                                Home
                            </Link>
                        </li>
                        <li className='nav-item'>
                            <Link
                                to='/about'
                                className='nav-links'
                                onClick={closeMobileMenu}
                            >
                                About us
                            </Link>
                        </li>
                        <li className='nav-item'>
                            <Link
                                to='/for-employers'
                                className='nav-links'
                                onClick={closeMobileMenu}
                            >
                                Information
                            </Link>
                        </li>
                    </ul>
                    {button && <Button buttonStyle='sides'>SIGN IN</Button>}
                </div>
            </nav>
        </>
    );
}

export default Navbar;

The Nav bar renders well but the hamburger doesn't show up when I reduce the window size. The CSS of the same is also below:

/* Your CSS code here */

Can anyone find the problem as it's been bugging me out.

英文:

I had implemented a nav bar in one of my websites, and when I tried to implement the same nav bar in another website I'm creating, the hamburger lines do not show up. I can't seem to find the problem in it. The code is in react (exactly like my old implementation) and the component's code is below:

import React, {useEffect, useState} from &#39;react&#39;;
import {Link} from &quot;react-router-dom&quot;;
import &#39;./Navbar.scss&#39;;
function Navbar() {
const [click, setClick] = useState(false);
const [button, setButton] = useState(true);
const handleClick = () =&gt; setClick(!click);
const closeMobileMenu = () =&gt; setClick(false);
const showButton = () =&gt; {
if (window.innerWidth &lt;= 960) {
setButton(false);
} else {
setButton(true);
}
};
useEffect(() =&gt; {
showButton();
}, []);
window.addEventListener(&#39;resize&#39;, showButton);
return (
&lt;&gt;
&lt;div className=&#39;header-container&#39;&gt;
&lt;p&gt;Book a visit by calling us 9&lt;/p&gt;
&lt;/div&gt;
&lt;nav className=&#39;navbar&#39;&gt;
&lt;div className=&#39;navbar-container&#39;&gt;
&lt;Link to=&#39;/&#39; className=&#39;navbar-logo&#39; onClick={closeMobileMenu}&gt;
LOGOG
&lt;/Link&gt;
{/*Below is the hamburger for the navigation bar*/}
&lt;div className=&#39;menu-icon&#39; onClick={handleClick}&gt;
&lt;i className={click ? &#39;fas fa-times&#39; : &#39;fas fa-bars&#39;} /&gt;
&lt;/div&gt;
&lt;ul className={click ? &#39;nav-menu active&#39; : &#39;nav-menu&#39;}&gt;
&lt;li className=&#39;nav-item&#39;&gt;
&lt;Link to=&#39;/&#39; className=&#39;nav-links&#39; onClick={closeMobileMenu}&gt;
Home
&lt;/Link&gt;
&lt;/li&gt;
&lt;li className=&#39;nav-item&#39;&gt;
&lt;Link
to=&#39;/about&#39;
className=&#39;nav-links&#39;
onClick={closeMobileMenu}
&gt;
About us
&lt;/Link&gt;
&lt;/li&gt;
&lt;li className=&#39;nav-item&#39;&gt;
&lt;Link
to=&#39;/for-employers&#39;
className=&#39;nav-links&#39;
onClick={closeMobileMenu}
&gt;
Information
&lt;/Link&gt;
&lt;/li&gt;
&lt;/ul&gt;
{button &amp;&amp; &lt;Button buttonStyle=&#39;sides&#39;&gt;SIGN IN&lt;/Button&gt;}
&lt;/div&gt;
&lt;/nav&gt;
&lt;/&gt;
);
}
export default Navbar;

The Nav bar renders well but the hamburger doesn't show up when I reduce the window size. The CSS of the same is also below:

.header-container {
display: flex;
height: 36px;
background: #D4ED31;
/*transform from above colour to #F0BF4C on hover*/
transition: background 0.5s ease-in-out;
justify-content: center;
text-align: center;
align-items: center;
}
.navbar {
background: linear-gradient(90deg, rgb(28, 27, 27) 0%, rgb(26, 23, 23) 100%);
height: 80px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.2rem;
position: sticky;
top: 0;
z-index: 999;
}
.navbar-container {
display: flex;
justify-content: center;
align-items: center;
height: 80px;
max-width: 1500px;
}
.navbar-logo {
color: #fff;
justify-self: start;
margin-left: 20px;
cursor: pointer;
text-decoration: none;
font-size: 2rem;
display: flex;
align-items: center;
}
.fa-typo3 {
margin-left: 0.5rem;
font-size: 1.8rem;
}
.nav-menu {
display: grid;
grid-template-columns: repeat(4, auto);
grid-gap: 10px;
list-style: none;
text-align: center;
width: 60vw;
justify-content: end;
margin-right: 2rem;
}
.nav-item {
height: 80px;
}
.nav-links {
color: #fff;
display: flex;
align-items: center;
text-decoration: none;
padding: 0.5rem 1rem;
height: 100%;
}
.nav-links:hover {
border-bottom: 4px solid #fff;
transition: all 0.2s ease-out;
}
.fa-bars {
color: #fff;
}
.nav-links-mobile {
display: none;
}
.menu-icon {
display: none;
}
@media screen and (max-width: 960px) {
.NavbarItems {
position: relative;
}
.nav-menu {
display: flex;
flex-direction: column;
width: 100%;
height: 90vh;
position: absolute;
top: 80px;
left: -100%;
opacity: 1;
transition: all 0.5s ease;
}
.nav-menu.active {
background: #242222;
left: 0;
opacity: 1;
transition: all 0.5s ease;
z-index: 1;
}
.nav-links {
text-align: center;
padding: 2rem;
width: 100%;
display: table;
}
.nav-links:hover {
background-color: #fff;
color: #242424;
border-radius: 0;
}
.navbar-logo {
position: absolute;
top: 0;
left: 0;
transform: translate(25%, 50%);
}
.menu-icon {
display: block;
position: absolute;
top: 0;
right: 0;
transform: translate(-100%, 60%);
font-size: 1.8rem;
cursor: pointer;
}
.fa-times {
color: #fff;
font-size: 2rem;
}
.nav-links-mobile {
display: block;
text-align: center;
margin: 2rem auto;
border-radius: 4px;
width: 80%;
text-decoration: none;
font-size: 1.5rem;
background-color: transparent;
color: #fff;
padding: 14px 20px;
border: 1px solid #fff;
transition: all 0.3s ease-out;
}
.nav-links-mobile:hover {
background: #fff;
color: #242424;
transition: 250ms;
}
}

Can anyone find the problem as it's been bugging me out.

答案1

得分: 0

The code looks fine. Can you check if you're importing the FA modules right? For starters add the below code in index.html and see if it works.

    <link
            rel="stylesheet"
            href="https://use.fontawesome.com/releases/v5.13.1/css/all.css"
            integrity="sha384-xxzQGERXS00kBmZW/6qxqJPyxW3UR0BPsL4c8ILaIWXva5kFi7TxkIIaMiKtqV1Q"
            crossorigin="anonymous"
    />

This may be the only reason you're not able to see the X and bars when reducing the window size as it's working on my end. Hope this helps!

英文:

The code looks fine. Can you check if you're importing the FA modules right? For starters add the below code in index.html and see if it works.

    &lt;link
rel=&quot;stylesheet&quot;
href=&quot;https://use.fontawesome.com/releases/v5.13.1/css/all.css&quot;
integrity=&quot;sha384-xxzQGERXS00kBmZW/6qxqJPyxW3UR0BPsL4c8ILaIWXva5kFi7TxkIIaMiKtqV1Q&quot;
crossorigin=&quot;anonymous&quot;
/&gt;

This may be the only reason you're not able to see the X and bars when reducing the window size as it's working on my end. Hope this helps!

huangapple
  • 本文由 发表于 2023年5月6日 16:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76187905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定