next.js 如何对齐响应导航栏中的按钮?

drnojrws  于 2023-06-22  发布在  其他
关注(0)|答案(1)|浏览(143)

我在“Navbar2.jsx”中有以下导航条形码:

'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;

这产生了“响应”导航条,我遇到的问题是移动的菜单上的下拉菜单将“X”按钮移到一边,我希望菜单只是“下拉”下面的汉堡包按钮变成“X”,而不移动。
以下是它现在的样子:

我尝试在按钮className中添加“absolute”,但没有任何改变。也尝试了“正义结束”,这也没有改变任何东西。
我已经将完整的项目上传到git here

pepwfjgg

pepwfjgg1#

考虑将<button>类从inline-flex更改为flex,使其成为块布局,然后将margin-left: automl-auto类一起应用,使<button>始终向右对齐:

<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>

相关问题