gpt4 book ai didi

javascript - 单击链接时,我试图关闭 React Bootstrap 中的 offcanvas 菜单

转载 作者:行者123 更新时间:2023-12-05 03:28:39 25 4
gpt4 key购买 nike

我正在使用 React Bootstrap 和 React 的组合来制作单页应用程序,我尝试了几种方法来在我单击链接时关闭 Offcanvas 菜单。我尝试在链接上创建一个内联脚本来切换菜单,但我发现菜单会按我希望的方式关闭,但链接只会将我带到它应该导航到的位置的一半。

到目前为止,这是我的代码:

import React from "react";
import { Navbar, Nav, Container, Offcanvas } from "react-bootstrap";
import { StaticImage } from "gatsby-plugin-image";
import styled from "styled-components";
import { Link } from "gatsby";

const Wrapper = styled.div`
background-color: #9ac2ba;
.Nav-Brand {
display: flex;
}
.navbar {
background-color: #9ac2ba;
}
`;

const LinkWrapper = styled.div`
margin-top: 20px;
font-size: 1.5rem;
text-align: center;
color: #333;
.nav-link {
color: #333;
}
.nav-link:hover {
color: #000;
}
`;

const Navigation = () => {
return (
<Wrapper>
<Navbar
as="nav"
variant="light"
fixed="top"
expand={false}
className="shadow"
>
<Container>
<Navbar.Brand href="/" className="Nav-Brand">
<StaticImage
src="../images/Spf-Brand-01.jpg"
alt="Brand Image"
layout="constrained"
placeholder="blurred"
height={50}
loading="eager"
/>
<h1 className="visually-hidden">
SPF Paint &amp; Decorating, Birmingham
</h1>
</Navbar.Brand>
<Navbar.Toggle
aria-controls="offcanvasNavbar"
aria-labelledby="offcanvasNavbarLabel"
/>
<Navbar.Offcanvas id="offcanvasNavbar" placement="start">
<Offcanvas.Header closeButton>
<Offcanvas.Title id="offcanvasNavbarLabel">
<span className="visually-hidden">SPF Nav Menu</span>
</Offcanvas.Title>
</Offcanvas.Header>
<Offcanvas.Body>
<Nav className="justify-content-end flex-grow-1 pe-3">
<StaticImage
src="../images/Spf-Brand-01.jpg"
alt="Brand Image"
layout="constrained"
placeholder="blurred"
height={50}
loading="eager"
className="offcanvas-brand"
/>
<LinkWrapper>
<Link to="/" className="nav-link">
Home
</Link>
<Link to="/#services" className="nav-link">
Services
</Link>
<Link to="/#faq" className="nav-link">
FAQ
</Link>
<Link to="/#contact" className="nav-link">
Contact
</Link>
</LinkWrapper>
</Nav>
</Offcanvas.Body>
</Navbar.Offcanvas>
</Container>
</Navbar>
</Wrapper>
);
};

export default Navigation;

这是网站的构建:https://pland.netlify.app/

最佳答案

您所有的链接都指向同一页面上的内容,因此使用 Link没有必要。相反,将它们替换为常规 anchor 标记 <a href=''>Link</a>

其次,Offcanvas 的默认行为是当覆盖层关闭时,焦点返回到覆盖层打开时的位置。这就是为什么在关闭叠加层时页面没有滚动到正确位置的原因。要更改此设置,您可以传入 restoreFocus={false}支持 Offcanvas。 See Offcanvas API docs .

第三,要让菜单在点击链接时关闭,你应该跟踪菜单是否处于打开状态,并在 onClick 中添加切换功能。每个链接的支柱,以及 Navbar.Toggle .

const Navigation = () => {
const [menuOpen, setMenuOpen] = useState(false)
const toggleMenu = () => {
setMenuOpen(!menuOpen)
}

const handleClose = () => setMenuOpen(false)

return(
/** Omit code */

<Navbar.Toggle
aria-controls='offcanvasNavbar'
aria-labelledby='offcanvasNavbarLabel'
/** Add onClick toggle here */
onClick={toggleMenu}
/>
<Navbar.Offcanvas
id='offcanvasNavbar'
placement='start'
/** Add these props */
restoreFocus={false}
show={menuOpen}
onHide={handleClose}
>

/** omit code */

<Nav className='justify-content-end flex-grow-1 pe-3'>
<a href='#services' className='nav-link' onClick={toggleMenu}>
Services
</a>
<a href='#faq' className='nav-link' onClick={toggleMenu}>
FAQ
</a>
{/** more links */}
</Nav>
/** omit code */
)
}

关于javascript - 单击链接时,我试图关闭 React Bootstrap 中的 offcanvas 菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71211745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com