我在& quotNavbar2.jsx & quot:
'use client';
import Link from 'next/link';
import Image from 'next/image';
import NavItem from './NavItem';
import { motion } from 'framer-motion';
import { navVariants } from '../utils/motion';
import styles from '../styles';
import { useState } from 'react';
import { Bars3Icon, XMarkIcon } from '@heroicons/react/24/outline';
import { Disclosure, Menu, Transition } from '@headlessui/react';
const menu_list = [
{ name: 'Home', href: '/', current: true },
{ name: 'About', href: '/', current: false },
{ name: 'Contact', href: '/', current: false },
]
function classNames(...classes) {
return classes.filter(Boolean).join(' ')
}
const Navbar2 = () => {
const [isOpen, setIsOpen] = useState(false);
return(
<motion.nav
variants={navVariants}
initial='hidden'
whileInView='show'
className={`${styles.xPaddings} py-8 relative z-30`}
>
<div className='absolute w-[50%] inset-0 gradient-01' />
<div className={`${styles.innerWidth} mx-auto flex justify-between gap-8`}>
<Link href='/' className='z-30 opacity-50 hover:opacity-100'>
<Image
src='/headset.svg'
alt='logo'
width={30}
height={30}
className='object-contain'
/>
</Link>
<h2 className='font-extrabold text-[24px] leading-[30px] text-white'>
TITLE
</h2>
<div className='hidden sm:flex space-x-4'>
{menu_list.map((item) => (
<Link href={item.href} className={classNames(
item.current ? 'bg-gray-900 text-white' : 'text-gray-300 hover:bg-gray-700 hover:text-white',
'rounded-md px-3 py-2 text-sm font-medium'
)}
aria-current={item.current ? 'page' : undefined}
>
{item.name}
</Link>
))}
</div>
<div>
<button type='button' onClick={() => setIsOpen(!isOpen)} className='sm:hidden inline-flex items-center justify-center rounded-md p-2 text-gray-400 hover:bg-gray-700 hover:text-white focus:outline-none focus:ring-2 focus:ring-inset focus:ring-white'>
<span className='sr-only'>Open main menu</span>
{isOpen ? (
<XMarkIcon className="block h-6 w-6 " aria-hidden="true" />
) : (
<Bars3Icon className="block h-6 w-6" aria-hidden="true" />
)}
</button>
{isOpen && (
<div className='space-y-1 px-2 pb-3 pt-2'>
<div className='rounded-md ring-1 ring-black ring-opacity-5 overflow-hidden'>
{menu_list.map((item) => (
<Link href={item.href} key={item.name} className={classNames(
item.current ? 'bg-gray-900 text-white' : 'text-gray-300 hover:bg-gray-700 hover:text-white',
'block px-4 py-2 text-sm'
)}
aria-current={item.current ? 'page' : undefined}
>
{item.name}
</Link>
))}
</div>
</div>
)}
</div>
</div>
</motion.nav>
)
}
export default Navbar2;
這產生了& quot響應性& quotnavbar,我遇到的問題是移動菜單上的下拉菜單移動了& quotX & quot按鈕,我希望菜單只是& quot下拉菜單& quot漢堡按鈕下面變成了一個& quotX & quot,不動。
現在看起來是這樣的:
我試著加上& quot絕對& quot但是它沒有改變任何東西。也試過& quot兩端對齊& quot這也沒有改變什么。
我已經在git上上傳了完整的項目
考慮更改& ltbutton & gt類從inline-flex轉換為flex,以便它成為一個塊布局,然后對ml-auto類應用margin-left: auto,以便& ltbutton & gt將始終向右對齊:
<script src="https://cdn.tailwindcss.com"></script>
<body class="bg-slate-950">
<nav class="py-8 relative z-30">
<div class="absolute w-[50%] inset-0 gradient-01"></div>
<div class="mx-auto flex justify-between gap-8">
<a href="/" class="z-30 opacity-50 hover:opacity-100">
<img src="https://picsum.photos/30/30" alt="logo" width="30" height="30" class="object-contain" />
</a>
<h2 class="font-extrabold text-[24px] leading-[30px] text-white">
TITLE
</h2>
<div>
<button type="button" class="flex items-center justify-center rounded-md p-2 text-gray-400 hover:bg-gray-700 hover:text-white focus:outline-none focus:ring-2 focus:ring-inset focus:ring-white ml-auto">
<span class="sr-only">Open main menu</span>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="block w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
<div class="space-y-1 px-2 pb-3 pt-2">
<div class="rounded-md ring-1 ring-black ring-opacity-5 overflow-hidden">
<a class="text-gray-300 hover:bg-gray-700 hover:text-white block px-4 py-2 text-sm">
{item.name}
</a>
</div>
</div>
</div>
</div>
</nav>