简体   繁体   English

如何在 nextjs 应用程序的导航栏上从左到右添加 Animation?

[英]How can I add Animation left to right on navbar in nextjs app?

I have a navbar and sidebar component in my nextjs app.我的 nextjs 应用程序中有一个navbarsidebar组件。 In my index component I'm using useState to show and hide sidebar on mobile device.在我的索引组件中,我使用useState在移动设备上显示和隐藏侧边栏。
It works perfectly fine but I want to add animation when user clicks on hamburger menu, the sidebar should be animated left to right and when clicked on close icon it should go back to right to left.它工作得很好,但我想在用户点击汉堡菜单时添加 animation,侧边栏应该从左到右动画,当点击关闭图标时,它应该 go 从右到左返回。 FYI I am using tailwind css.仅供参考,我正在使用顺风 css。

Here's the code:这是代码:
indexjs file: index.js 文件:

export default function Home() {
 const [sidebar, setSidebar] = useState(false);
return(
<>
        <Navbar sidebar={sidebar} setSidebar={setSidebar} />
        {sidebar ? (
          <div className={sidebar ? "transform-x-0" : "transform-x-full"}>
            <Sidebar sidebar={sidebar} setSidebar={setSidebar} />
          </div>
        ) : null}
</>

)

Navbar.js:导航栏.js:


const Navbar = ({ sidebar, setSidebar }) => {
  return (
    <div>
      <header className="px-4 max-w-desktop mx-auto text-blacklight">
        <nav
          className="
          flex
          lg:flex-row
          items-center
          flex-auto
          justify-between
          lg:mx-auto
          md:py-6
          py-4
          relative
          navigation
        "
          id="navigation"
        >
          <div className="flex flex-row items-center">
            <img src="./assets/logo.svg" alt="fab systems" />
            <h2 className="text-lg font-extrabold flex-none ml-1 leading-none">
              FAB <br />
              SYSTEMS
            </h2>
          </div>
          <ul
            className="
            lg:flex lg:flex-row
            flex-col
            max-w-full
            lg:w-2/3
            mt-4
            lg:mt-0
            hidden
            lg:items-center
            justify-between
            flex-none
          "
          >
            <li className="menu-links">
              <a href="./features.html">Features</a>
            </li>
            <li className="menu-links">
              <a href="./pricing.html">Pricing</a>
            </li>
            <li className="menu-links">
              <a href="./customer-result.html">Customer Results</a>
            </li>
            <li className="menu-links">
              <a href="./about.html">About</a>
            </li>
            <li className="menu-links">
              <a href="#">Blog</a>
            </li>
            <li className="menu-links">
              <a href="#">Support</a>
            </li>
            <div className="flex flex-col">
              <li
                className="
                menu-links
                bg-dark-blue
                py-2
                rounded-full
                text-white
                font-semibold
                px-4
              "
              >
                <a href="#">Free Demo</a>
              </li>
              <p className="absolute right-0 w-screen text-right bottom-0 text-sm">
                or call sales
                <a href="https://twitter.com/" className="text-blue">
                  +91 982-488-5838
                </a>
              </p>
            </div>
          </ul>
          <div className="lg:hidden">
            <svg
              className="w-8 h-8 lg:hidden"
              id="hamburger"
              onClick={() => setSidebar(true)}
              fill="none"
              stroke="#354650"
              viewBox="0 0 24 24"
              xmlns="http://www.w3.org/2000/svg"
            >
              <path
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="2"
                d="M4 6h16M4 12h16M4 18h16"
              ></path>
            </svg>
          </div>
        </nav>
      </header>
    </div>
  );
};

export default Navbar;

Sidebar.js:侧边栏.js:

import React from "react";

const Sidebar = ({ sidebar, setSidebar }) => {
  const hideSidebar = () => {
    setSidebar(!sidebar);
  };

  return (
    <div
      className="
        overflow-x-hidden
        min-h-screen
        absolute
        z-50
        bg-dark-green
        lg:px-12
        px-4
        md:py-6
        py-4
        transform
        left-0
        w-screen
        transition
        duration-300
        top-0
      "
      id="mobile-nav"
    >
      <div className="flex flex-row justify-between items-center">
        <div>
          <img src="./assets/logo.svg" alt="fab systems" />
        </div>
        <svg
          className="w-8 h-8 lg:hidden mr-2"
          id="close"
          onClick={hideSidebar}
          fill="none"
          stroke="#FFFFFF"
          viewBox="0 0 24 24"
          xmlns="http://www.w3.org/2000/svg"
        >
          <path
            stroke-linecap="round"
            stroke-linejoin="round"
            stroke-width="2"
            d="M6 18L18 6M6 6l12 12"
          ></path>
        </svg>
      </div>
      <ul className="text-xl text-white my-4">
        <li className="menu-links py-4 border-gray-600 border-b-2">
          <a href="./features.html">Features</a>
        </li>
        <li className="menu-links py-4 border-gray-600 border-b-2">
          <a href="./pricing.html">Pricing</a>
        </li>
        <li className="menu-links py-4 border-gray-600 border-b-2">
          <a href="./customer-result.html">Customer Results</a>
        </li>
        <li className="menu-links py-4 border-gray-600 border-b-2">
          <a href="./about.html">About</a>
        </li>
        <li className="menu-links py-4 border-gray-600 border-b-2">
          <a href="#">Blog</a>
        </li>
        <li className="menu-links py-4 border-gray-600 border-b-2">
          <a href="#">Support</a>
        </li>
        <li className="menu-links py-4">
          <a href="#">Free Demo</a>
        </li>
      </ul>
      <div className="relative py-1 w-screen flex flex-row pr-4">
        <input
          type="email"
          className="
            p-2
            px-3
            border-gray-400
            rounded-tl rounded-bl
            border
            text-sm
            relative
            max-w-full
            w-full
          "
          placeholder="Enter your phone"
        />
        <button
          className="
            py-2
            text-white
            rounded-tr-full rounded-br-full
            bg-dark-blue
            text-sm
            px-4
            mr-4
            sm:w-1/4
            w-1/2
            border border-blue-600
            -ml-2
          "
        >
          Try Free Demo
        </button>
      </div>
    </div>
  );
};

export default Sidebar;

Can you try this?你能试试这个吗?

<div className={`sidebar ${sidebar ? "transform-x-0" : "transform-x-full"}`}>

And give .sidebar transition?并给.sidebar过渡?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的网络表单中从右到左导航栏? - How Can I Make Right To Left Navbar In My Webform? 如何在导航栏的左侧或中心添加徽标? - How can I add a logo to the left or the center of my navbar? 如何在导航栏中将居中链接和其他右/左位置的链接居中? - How can I have centered links and other right/left-positioned ones in a navbar? 如何使用引导程序将徽标居中并在导航栏上左右放置链接? - How can I center a logo and place the links left and right on the navbar using bootstrap? 如何将导航栏项目与居中图像的左侧和右侧对齐? - How can I align my navbar items to the left and right side of the centered image? 如何在面板标题的左侧和右侧添加文本? - How can I add text on the left and right of my Panel Title? 如何在鼠标悬停时使用css或bootstrap从右到左添加动画? - how to add animation right to left using css or bootstrap on mouse hover? 如何实现从右到左,从下到上的文本键入动画? - How I can realize an animation of text typing from right to left and from bottom to top? 我在网站的导航栏中使用border-left,border-right作为分隔线。 我想知道如何将它们放在导航栏中 - I'm using border-left border-right as dividers in my site's navbar. I want to know how I can have them centered in the navbar 我如何将导航栏元素移动到左侧,除了导航栏的“帐户”元素,并将其放在右侧? - How would i move navbar elements to the left side, except the “Account” element of the navbar, and put it to the right side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM