代码

  1. "use client";
  2. import React, { useState } from "react";
  3. const Navbar = () => {
  4. const [isOpen, setIsOpen] = useState(true);
  5. const toggleNavbar = () => {
  6. setIsOpen(!isOpen);
  7. };
  8. return (
  9. <nav
  10. className={`duration-800 fixed bottom-0 left-1/2 flex
  11. w-1/2 -translate-x-1/2 transform items-center justify-between rounded-2xl bg-gray-800 p-4 text-white transition-all ${isOpen ? "h-16" : "h-16 w-fit"}`}
  12. >
  13. <div className={`flex space-x-4 ${isOpen ? "" : "hidden"}`}>
  14. <button className="rounded-full bg-blue-500 p-2 hover:bg-blue-600 focus:outline-none">
  15. <svg
  16. className="h-6 w-6"
  17. fill="none"
  18. stroke="currentColor"
  19. viewBox="0 0 24 24"
  20. xmlns="http://www.w3.org/2000/svg"
  21. >
  22. <path
  23. strokeLinecap="round"
  24. strokeLinejoin="round"
  25. strokeWidth="2"
  26. d="M5 13l4 4L19 7"
  27. ></path>
  28. </svg>
  29. </button>
  30. <button className="rounded-full bg-blue-500 p-2 hover:bg-blue-600 focus:outline-none">
  31. <svg
  32. className="h-6 w-6"
  33. fill="none"
  34. stroke="currentColor"
  35. viewBox="0 0 24 24"
  36. xmlns="http://www.w3.org/2000/svg"
  37. >
  38. <path
  39. strokeLinecap="round"
  40. strokeLinejoin="round"
  41. strokeWidth="2"
  42. d="M5 13l4 4L19 7"
  43. ></path>
  44. </svg>
  45. </button>
  46. <button className="rounded-full bg-blue-500 p-2 hover:bg-blue-600 focus:outline-none">
  47. <svg
  48. className="h-6 w-6"
  49. fill="none"
  50. stroke="currentColor"
  51. viewBox="0 0 24 24"
  52. xmlns="http://www.w3.org/2000/svg"
  53. >
  54. <path
  55. strokeLinecap="round"
  56. strokeLinejoin="round"
  57. strokeWidth="2"
  58. d="M5 13l4 4L19 7"
  59. ></path>
  60. </svg>
  61. </button>
  62. </div>
  63. <div className={`flex space-x-4 ${isOpen ? "" : "hidden"}`}>
  64. <button className="rounded-full bg-blue-500 p-2 hover:bg-blue-600 focus:outline-none">
  65. <svg
  66. className="h-6 w-6"
  67. fill="none"
  68. stroke="currentColor"
  69. viewBox="0 0 24 24"
  70. xmlns="http://www.w3.org/2000/svg"
  71. >
  72. <path
  73. strokeLinecap="round"
  74. strokeLinejoin="round"
  75. strokeWidth="2"
  76. d="M5 13l4 4L19 7"
  77. ></path>
  78. </svg>
  79. </button>
  80. <button className="rounded-full bg-blue-500 p-2 hover:bg-blue-600 focus:outline-none">
  81. <svg
  82. className="h-6 w-6"
  83. fill="none"
  84. stroke="currentColor"
  85. viewBox="0 0 24 24"
  86. xmlns="http://www.w3.org/2000/svg"
  87. >
  88. <path
  89. strokeLinecap="round"
  90. strokeLinejoin="round"
  91. strokeWidth="2"
  92. d="M5 13l4 4L19 7"
  93. ></path>
  94. </svg>
  95. </button>
  96. </div>
  97. <button
  98. className="rounded-full bg-blue-500 p-2 hover:bg-blue-600 focus:outline-none"
  99. onClick={toggleNavbar}
  100. >
  101. <svg
  102. className="h-6 w-6"
  103. fill="none"
  104. stroke="currentColor"
  105. viewBox="0 0 24 24"
  106. xmlns="http://www.w3.org/2000/svg"
  107. >
  108. <path
  109. strokeLinecap="round"
  110. strokeLinejoin="round"
  111. strokeWidth="2"
  112. d={isOpen ? "M19 9l-7 7-7-7" : "M4 9l7 7 7-7"}
  113. ></path>
  114. </svg>
  115. </button>
  116. </nav>
  117. );
  118. };
  119. export default Navbar;

演示

打开导航栏:

实现一个mac-PC风格的浮空导航栏 - 图1

关闭导航栏:

实现一个mac-PC风格的浮空导航栏 - 图2

优化-新增放大效果

接下来在图标上加入一个鼠标悬停时放大的效果。

  1. "use client";
  2. import React, { useState } from "react";
  3. const Navbar = () => {
  4. const [isOpen, setIsOpen] = useState(true);
  5. const toggleNavbar = () => {
  6. setIsOpen(!isOpen);
  7. };
  8. return (
  9. <nav
  10. className={`fixed bottom-0 left-1/2 flex w-1/2 -translate-x-1/2 transform items-center justify-between rounded-2xl bg-gray-800 p-4 text-white transition duration-700 ease-in-out ${isOpen ? "h-16" : "h-16 w-fit"}`}
  11. >
  12. <div className={`flex space-x-4 transition-opacity duration-700 ease-in-out ${isOpen ? "opacity-100" : "opacity-0"}`}>
  13. {Array.from({ length: 5 }).map((_, index) => (
  14. <button
  15. key={index}
  16. className="rounded-full bg-blue-500 p-2 hover:bg-blue-600 focus:outline-none transform transition-transform duration-200 hover:scale-125"
  17. >
  18. <svg
  19. className="h-6 w-6"
  20. fill="none"
  21. stroke="currentColor"
  22. viewBox="0 0 24 24"
  23. xmlns="http://www.w3.org/2000/svg"
  24. >
  25. <path
  26. strokeLinecap="round"
  27. strokeLinejoin="round"
  28. strokeWidth="2"
  29. d="M5 13l4 4L19 7"
  30. ></path>
  31. </svg>
  32. </button>
  33. ))}
  34. </div>
  35. <button
  36. className="rounded-full bg-blue-500 p-2 hover:bg-blue-600 focus:outline-none"
  37. onClick={toggleNavbar}
  38. >
  39. <svg
  40. className="h-6 w-6"
  41. fill="none"
  42. stroke="currentColor"
  43. viewBox="0 0 24 24"
  44. xmlns="http://www.w3.org/2000/svg"
  45. >
  46. <path
  47. strokeLinecap="round"
  48. strokeLinejoin="round"
  49. strokeWidth="2"
  50. d={isOpen ? "M19 9l-7 7-7-7" : "M4 9l7 7 7-7"}
  51. ></path>
  52. </svg>
  53. </button>
  54. </nav>
  55. );
  56. };
  57. export default Navbar;
  1. 添加过渡效果
    • 将鼠标悬停效果添加在按钮的className中:transform transition-transform duration-200 hover:scale-125
    • transformtransition-transform 用于开启变换和过渡效果。
    • duration-200 设置了过渡效果的持续时间为200毫秒。
    • hover:scale-125 设置了悬停时的放大比例为1.25倍。