繁体   English   中英

Tailwind CSS 导航栏

[英]Tailwind CSS Navbar

无论如何,我无法让这个导航栏正确折叠。 我需要在响应模式下将导航栏从 flex 更改,但它目前无法正常工作。

HTML:

 <nav class="flex items-center justify-between flex-wrap bg-blue-darkest p-6 z-10">
    <div class="flex items-center flex-no-shrink 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">Tailwind CSS</span>
    </div>
    <div class="block lg:hidden">
      <button class="flex items-center px-3 py-2 border rounded text-white border-white hover:text-blue-darkest hover:bg-white"
        onclick="myFunction()">
        <svg class="fill-current h-3 w-3" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
          <title>Menu</title>
          <path d="M0 3h20v2H0V3zm0 6h20v2H0V9zm0 6h20v2H0v-2z" />
        </svg>
      </button>
    </div>
    <div class="w-full hidden lg:block flex-grow lg:flex lg:items-center lg:w-auto" id="nav">
      <div class="text-sm lg:flex-grow">
        <a href="#responsive-header" class="block mt-4 lg:inline-block lg:mt-0 text-teal-lighter hover:text-white mr-4">
          Docs
        </a>
        <a href="#responsive-header" class="block mt-4 lg:inline-block lg:mt-0 text-teal-lighter hover:text-white mr-4">
          Examples
        </a>
        <a href="#responsive-header" class="block mt-4 lg:inline-block lg:mt-0 text-teal-lighter 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 hover:bg-white mt-4 lg:mt-0">Download</a>
      </div>
    </div>
  </nav>

JavaScript 代码:

function myFunction() {
    var x = document.getElementById("nav");
    if (x.className === "hidden") {
        x.classList.toggle("hidden");
    } else {
        console.log("nav");
        x.className = "hidden";
    }
}

我该如何解决这个问题?

也完全可以在显示元素上使用 Tailwind CSS 的响应式修饰符。

您可以更改nav元素的类,而不仅仅是将flex更改为md:flex block或类似的内容。 当然,您需要在实际站点上调整正确的显示值和响应实用程序是通过进行一些试验。

来自 Tailwind CSS wiki 的引用指出:

要在特定断点处控制元素的显示属性,请向任何现有的显示实用程序类添加{screen}:前缀。 例如,使用md:inline-flex仅在中等屏幕尺寸及以上尺寸时应用 inline-flex 实用程序。

您可以在 Tailwind CSS wiki 中阅读更多内容。

除此之外,还有一个建议:您可以将myFunction缩减为仅四行代码。 目前,您的 if-else 语句只是在做classList.toggle已经做的事情,但是是手动的。 如果检查是因为您想检查您是否获得了正确的元素,那么请注意,您永远不应该在单个页面上有多个具有相同 id 的元素

function myFunction() {
    var x = document.getElementById("nav");
    x.classList.toggle("hidden");
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM