使用Tailwind CSS平滑显示移动菜单。

huangapple go评论80阅读模式
英文:

showing the mobile menu smoothly using tailwindcss

问题

要使移动菜单在单击汉堡菜单时平滑显示,您可以使用Tailwind CSS的过渡效果。在您的JavaScript代码中,您已经尝试添加过渡类,但需要稍作修改。下面是修改后的JavaScript代码:

const btn = document.getElementById("show-menu");
const menu = document.querySelector(".mobile-menu");
btn.addEventListener("click", () => {
  menu.classList.toggle("hidden");
  setTimeout(() => {
    menu.classList.toggle("transition");
    menu.classList.toggle("delay-150");
    menu.classList.toggle("duration-300");
  }, 0);
});

这里使用了setTimeout函数,将过渡类的添加延迟到下一个渲染帧,以确保过渡效果生效。现在,当您点击汉堡菜单时,移动菜单应该会以平滑的方式显示出来。

英文:

I want to show the mobile menu smoothly. Right now I'm using tailwindcss and when I click on the hamburger icon, the menu comes out instantly. I tried adding delay but it isn't doing anything. How would I make the mobile menu show smoothly when I click on the hamburger menu

<body>
    <nav class="flex items-center justify-between flex-wrap bg-teal-500 p-6">
        <div class="flex items-center flex-shrink-0 text-white mr-6">
          <svg class="fill-current h-8 w-8 mr-2" width="54" height="54" viewBox="0 0 54 54" xmlns="http://www.w3.org/2000/svg"><path d="M13.5 22.1c1.8-7.2 6.3-10.8 13.5-10.8 10.8 0 12.15 8.1 17.55 9.45 3.6.9 6.75-.45 9.45-4.05-1.8 7.2-6.3 10.8-13.5 10.8-10.8 0-12.15-8.1-17.55-9.45-3.6-.9-6.75.45-9.45 4.05zM0 38.3c1.8-7.2 6.3-10.8 13.5-10.8 10.8 0 12.15 8.1 17.55 9.45 3.6.9 6.75-.45 9.45-4.05-1.8 7.2-6.3 10.8-13.5 10.8-10.8 0-12.15-8.1-17.55-9.45-3.6-.9-6.75.45-9.45 4.05z"/></svg>
          <span class="font-semibold text-xl tracking-tight">Menu</span>
        </div>
        <div class="block lg:hidden">
          <button class="flex items-center px-3 py-2 border rounded text-teal-200 border-teal-400 hover:text-white hover:border-white" id="show-menu">
            <svg class="fill-current h-3 w-3" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M0 3h20v2H0V3zm0 6h20v2H0V9zm0 6h20v2H0v-2z"/></svg>
          </button>
        </div>
        <div class="w-full block flex-grow lg:flex lg:items-center lg:w-auto mobile-menu">
          <div class="text-sm lg:flex-grow">
            <a href="#responsive-header" class="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white mr-4">
              Docs
            </a>
            <a href="#responsive-header" class="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white mr-4">
              Examples
            </a>
            <a href="#responsive-header" class="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white">
              Blog
            </a>
          </div>
          <div>
            <a href="#" class="inline-block text-sm px-4 py-2 leading-none border rounded text-white border-white hover:border-transparent hover:text-teal-500 hover:bg-white mt-4 lg:mt-0">Download</a>
          </div>
        </div>
      </nav>

    <!-- JavaScript -->
    <script src="script.js"></script>
</body>

js

const btn = document.getElementById("show-menu");
const menu = document.querySelector(".mobile-menu");
btn.addEventListener("click", () => {
  menu.classList.toggle("hidden");
  menu.classList.toggle("transition")
  menu.classList.toggle("delay-150")
  menu.classList.toggle("duration-300")
});

答案1

得分: 1

以下是翻译好的内容:

您的方法的解决方案

由于在将元素的CSS widthheight 设置为 auto 时无法进行动画处理。请查看关于这个问题的这个帖子

  1. 在您的 div.mobile-menu 上添加 overflow-hidden
  2. max-h-0 作为初始高度。
  3. 切换 max-h-0max-h-40 以动画显示菜单高度。

在这个Codesandbox上查看。

动画DOM元素大小的现代方法

使用以下方法计算并保存元素的默认(自动)宽度或高度:

  1. 使用ResizeObserver API
  2. 使用getBoudingClientRect()

请记住设置 overflow: hidden; 以剪切它们的内容。
然后,您可以通过以下方法对它们进行动画处理:

  1. 使用JavaScript将它们的宽度或高度值设置为默认值和0之间。
  2. 为将要进行动画处理的元素设置CSS Transition。切换它们的宽度或高度在默认值和0之间。
英文:

The solution to your approach

Since you can't animate element CSS width or height when it set to auto. Check out this thread on this problem.

  1. Added overflow-hidden to your div.mobile-menu.
  2. Add max-h-0 as the initial height.
  3. Toggle max-h-0 and max-h-40 to animate the menu height.

Check it out at this Codesandbox

Morden approachs for animating DOM element size

Calculate & save the element default (auto) width or height using:

  1. Use ResizeObserver API.
  2. Use getBoudingClientRect().

Remember to set overflow: hidden; to clip their content.
Then, you can animate them by:

  1. Use Javascript to set their width or height value between the default value and 0.
  2. Set CSS Transition to element that will be animated. Toggle their width or height between the default value and 0.

答案2

得分: 0

.mobile-menu {
  transition-property: max-height;
  max-height: 0;
  overflow: hidden;
}

.mobile-menu.transition-all {
  transition-duration: 300ms;
}
const btn = document.getElementById("show-menu");
const menu = document.querySelector(".mobile-menu");
btn.addEventListener("click", () => {
  menu.classList.toggle("hidden");
  menu.classList.toggle("transition-all");
  menu.classList.toggle("duration-300");
});
英文:
.mobile-menu {
  transition-property: max-height;
  max-height: 0;
  overflow: hidden;
}

.mobile-menu.transition-all {
  transition-duration: 300ms;
}
const btn = document.getElementById("show-menu");
const menu = document.querySelector(".mobile-menu");
btn.addEventListener("click", () => {
  menu.classList.toggle("hidden");
  menu.classList.toggle("transition-all");
  menu.classList.toggle("duration-300");
});

huangapple
  • 本文由 发表于 2023年5月10日 13:59:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定