如何在react jsx中使汉堡菜单适合整个页面
P粉368878176
P粉368878176 2023-08-31 14:31:09
[React讨论组]
<p>我对 React jsx 很陌生,我正在使用 Tailwind,它非常好。我也在边走边学。因此,在我的导航中,我能够显示汉堡菜单,但我不喜欢它们的显示方式。我试图确保整个页面都充满白色页面,请参见下面的第二张图片。</p> <p>这就是我能够实现的目标,但我认为它看起来不太干净</p> <p>我想要实现的目标,或类似的目标,白色填充整个网站并显示选项</p> <p>我现在的问题是,用我的代码;我怎样才能做到这一点?这是我的导航 jsx,我将不胜感激任何帮助。我是唯一的团队成员,没有首席开发人员可以提供帮助。</p> <pre class="brush:php;toolbar:false;">import logo from &quot;./favicon.png&quot;; import { motion } from &quot;framer-motion&quot;; import { useInView } from &quot;react-intersection-observer&quot;; import MenuDropdown from &quot;./MenuDropdown&quot;; import { Link } from &quot;react-router-dom&quot;; import { useState } from &quot;react&quot;; export default function Navigation() { const [ref, inView] = useInView({ triggerOnce: false }); const [isOpen, setIsOpen] = useState(false); const [navVisible, setNaVisible] = useState(false); const megaMenu = document.getElementById(&quot;mega-menu&quot;); return ( &lt;motion.nav className=&quot;flex items-center justify-between flex-wrap px-6 lg:px-12 py-8 max-w-full mx-auto&quot; ref={ref} initial={{ y: -10, opacity: 0 }} animate={inView ? { y: 0, opacity: 1 } : {}} exit={{ y: -10, opacity: 0 }} transition={{ duration: 0.5 }} &gt; &lt;a href=&quot;/&quot; className=&quot;flex flex-row items-center flex-shrink-0 text-white mr-6 lg:pr-16 space-x-2&quot; &gt; &lt;img src={logo} className=&quot;w-8 h-8&quot; alt=&quot;Logo&quot;&gt;&lt;/img&gt; &lt;h2 className=&quot;font-bold text-2xl tracking-tight&quot;&gt;Jibu Labs&lt;/h2&gt; &lt;/a&gt; &lt;div id=&quot;menu&quot; className=&quot;block lg:hidden&quot;&gt; &lt;button className=&quot;flex items-center px-3 py-2 border rounded text-slate-200 border-slate-400 duration-100 hover:text-white hover:border-white&quot; onClick={() =&gt; setNaVisible(!navVisible)} &gt; &lt;svg className=&quot;fill-current h-3 w-3&quot; viewBox=&quot;0 0 20 20&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; &gt; &lt;title&gt;Menu&lt;/title&gt; &lt;path d=&quot;M0 3h20v2H0V3zm0 6h20v2H0V9zm0 6h20v2H0v-2z&quot; /&gt; &lt;/svg&gt; &lt;/button&gt; &lt;/div&gt; &lt;div className={` ${ navVisible ? &quot;w-full block flex-grow g:flex lg:items-center lg:w-auto&quot; : &quot;lg:flex flex-none w-6/12 hidden lg:content-right&quot; }`} &gt; &lt;div className={` ${ navVisible ? &quot;lg:flex-grow flex flex-col text-sm&quot; : &quot;lg:flex-grow flex flex-rows space-x-12 text-sm &quot; }`} &gt; &lt;div className={`${ !navVisible ? &quot;flex flex-rows items-center space-x-2 text-white&quot; : &quot;flex flex-rows items-center space-x-2 text-white justify-end pr-12&quot; }`} &gt; &lt;span className=&quot;ring-1 p-0.5 ring-white rounded-full&quot;&gt;&lt;/span&gt; &lt;Link to=&quot;projects&quot; className={`${ !navVisible ? &quot;block mt-4 lg:inline-block lg:mt-0 text-white mr-4&quot; : &quot;block mt-4 lg:inline-block lg:mt-0 text-white mr-4 justify-end &quot; }`} &gt; Work &lt;/Link&gt; &lt;/div&gt; {!navVisible ? ( &lt;div id=&quot;mega-menu&quot; className={`flex items-center space-x-2 flex-rows text-white `} onClick={() =&gt; setIsOpen((prev) =&gt; !prev)} &gt; &lt;span className=&quot;ring-1 p-0.5 ring-white rounded-full&quot;&gt;&lt;/span&gt; &lt;div className=&quot;block mt-4 lg:inline-block lg:mt-0 mr-4 cursor-pointer&quot;&gt; Effeciencies &lt;/div&gt; &lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; fill=&quot;none&quot; viewBox=&quot;0 0 24 24&quot; strokeWidth={1} stroke=&quot;currentColor&quot; className=&quot;w-4 h-4&quot; &gt; &lt;path strokeLinecap=&quot;round&quot; strokeLinejoin=&quot;round&quot; d=&quot;M19.5 8.25l-7.5 7.5-7.5-7.5&quot; /&gt; &lt;/svg&gt; &lt;/div&gt; ) : ( &lt;div className={`${ !navVisible ? &quot;flex flex-rows items-center space-x-2 text-white&quot; : &quot;flex flex-rows items-center space-x-2 text-white justify-end&quot; }`} &gt; &lt;span className=&quot;ring-1 p-0.5 ring-white rounded-full&quot;&gt;&lt;/span&gt; &lt;Link to=&quot;strategy&quot; className=&quot;block mt-4 lg:inline-block lg:mt-0 text-white mr-4 justify-end pr-1&quot; &gt; Effeciencies &lt;/Link&gt; &lt;/div&gt; )} &lt;div className={`${ !navVisible ? &quot;flex flex-rows items-center space-x-2 text-white&quot; : &quot;flex flex-rows items-center space-x-2 text-white justify-end pr-10&quot; }`} &gt; &lt;span className=&quot;ring-1 p-0.5 ring-white rounded-full&quot;&gt;&lt;/span&gt; &lt;a href=&quot;about&quot; className=&quot;block mt-4 lg:inline-block lg:mt-0 text-white mr-4&quot; &gt; About &lt;/a&gt; &lt;/div&gt; &lt;div className={`${ !navVisible ? &quot;flex flex-rows items-center space-x-2 text-white&quot; : &quot;flex flex-rows items-center space-x-2 text-white justify-end pr-7&quot; }`} &gt; &lt;span className=&quot;ring-1 p-0.5 ring-white rounded-full&quot;&gt;&lt;/span&gt; &lt;a href=&quot;careers&quot; className=&quot;block mt-4 lg:inline-block lg:mt-0 text-white mr-7&quot; &gt; Careers &lt;/a&gt; &lt;/div&gt; &lt;div className={`${ !navVisible ? &quot;flex flex-rows items-center space-x-2 text-white&quot; : &quot;flex flex-rows items-center space-x-2 text-white justify-end pr-7&quot; }`} &gt; &lt;span className=&quot;ring-1 p-0.5 ring-white rounded-full&quot;&gt;&lt;/span&gt; &lt;a href=&quot;contact&quot; className=&quot;block mt-4 lg:inline-block lg:mt-0 text-white mr-4&quot; &gt; Contact &lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;MenuDropdown isOpen={isOpen} setIsOpen={setIsOpen} /&gt; &lt;/motion.nav&gt; ); }</pre></p>
P粉368878176
P粉368878176

全部回复(1)
P粉596161915

要用白色或其他背景颜色填充整个页面,您需要一个外部 div,将高度设置为 100vh,宽度设置为 100vw。 100vh 表示将高度设置为 Web 浏览器屏幕视口高度的 100%,100vw 表示将高度设置为视口宽度的 100%。

return (
   <div style={{height: "100vh", width: "100vw", maxWidth: "100%", background: "white"}}>
    <motion.nav
      className="flex items-center justify-between flex-wrap px-6 lg:px-12 
   ...
    </motion.nav>
  </div>

将 maxWidth 设置为 100% 是为了防止在 body 标签有 padding 或有其他外部组件占用空间时显示滚动条。

以上使得motion.nav组件占据整个页面。您可能希望将整个页面 div 向上移动到使用该组件的 JSX。

希望这有帮助。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号