【发布时间】:2021-05-28 16:26:12
【问题描述】:
我正在尝试使用 tailwind 框架创建一个带有下拉菜单的简单导航栏。
这是我的 sn-p:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js" defer=""></script>
<nav class="bg-gray-800 fixed w-full z-10 top-0">
<div class="max-w-7xl mx-auto px-2 sm:px-6 lg:px-8">
<div class="relative flex items-center justify-between h-16">
<div class="absolute inset-y-0 left-0 flex items-center sm:hidden">
<!-- Mobile menu button-->
<button type="button" class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-white hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-white" aria-controls="mobile-menu" aria-expanded="false">
<span class="sr-only">Open main menu</span>
<!-- Icon when menu is closed. -->
<!--
Heroicon name: outline/menu
Menu open: "hidden", Menu closed: "block"
-->
<svg class="block h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
<!-- Icon when menu is open. -->
<!--
Heroicon name: outline/x
Menu open: "block", Menu closed: "hidden"
-->
<svg class="hidden h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
</div>
<div class="flex-1 flex items-center justify-center sm:items-stretch sm:justify-start">
<div class="flex-shrink-0 flex items-center">
<img class="block lg:hidden h-8 w-auto" src="https://tailwindui.com/img/logos/workflow-mark-indigo-500.svg" alt="Workflow">
<img class="hidden lg:block h-8 w-auto" src="https://tailwindui.com/img/logos/workflow-logo-indigo-500-mark-white-text.svg" alt="Workflow">
</div>
<div class="hidden sm:block mx-auto">
<div class="flex space-x-4">
<!-- Current: "bg-gray-900 text-white", Default: "text-gray-300 hover:bg-gray-700 hover:text-white" -->
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium">Menu 1</a>
<a href="#" class="bg-gray-900 text-white px-3 py-2 rounded-md text-sm font-medium">Menu 2</a>
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium">Menu 3</a>
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium">Menu 4</a>
</div>
</div>
</div>
<div class="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0">
<!-- Notifications dropdown -->
<div class="ml-3 relative">
<div x-data="{ dropdownOpen: false }" class="relative my-32">
<button @click="dropdownOpen = !dropdownOpen" class="relative z-10 block rounded-md bg-white p-2 focus:outline-none">
<svg class="h-5 w-5 text-gray-800" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<path d="M10 2a6 6 0 00-6 6v3.586l-.707.707A1 1 0 004 14h12a1 1 0 00.707-1.707L16 11.586V8a6 6 0 00-6-6zM10 18a3 3 0 01-3-3h6a3 3 0 01-3 3z"></path>
</svg>
</button>
<div x-show="dropdownOpen" @click="dropdownOpen = false" class="fixed inset-0 h-full w-full z-10" style="display: none;"></div>
<div x-show="dropdownOpen" class="absolute right-0 mt-2 bg-white rounded-md shadow-lg overflow-hidden z-20" style="width: 20rem; display: none;">
<div class="py-2">
<a href="#" class="flex items-center px-4 py-3 border-b hover:bg-gray-100 -mx-2">
<img class="h-8 w-8 rounded-full object-cover mx-1" src="https://images.unsplash.com/photo-1494790108377-be9c29b29330?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80" alt="avatar">
<p class="text-gray-600 text-sm mx-2">
<span class="font-bold" href="#">Sara Salah</span> replied on the <span class="font-bold text-blue-500" href="#">Upload Image</span> artical . 2m
</p>
</a>
<a href="#" class="flex items-center px-4 py-3 border-b hover:bg-gray-100 -mx-2">
<img class="h-8 w-8 rounded-full object-cover mx-1" src="https://images.unsplash.com/photo-1531427186611-ecfd6d936c79?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80" alt="avatar">
<p class="text-gray-600 text-sm mx-2">
<span class="font-bold" href="#">Slick Net</span> start following you . 45m
</p>
</a>
<a href="#" class="flex items-center px-4 py-3 border-b hover:bg-gray-100 -mx-2">
<img class="h-8 w-8 rounded-full object-cover mx-1" src="https://images.unsplash.com/photo-1450297350677-623de575f31c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80" alt="avatar">
<p class="text-gray-600 text-sm mx-2">
<span class="font-bold" href="#">Jane Doe</span> Like Your reply on <span class="font-bold text-blue-500" href="#">Test with TDD</span> artical . 1h
</p>
</a>
<a href="#" class="flex items-center px-4 py-3 hover:bg-gray-100 -mx-2">
<img class="h-8 w-8 rounded-full object-cover mx-1" src="https://images.unsplash.com/photo-1580489944761-15a19d654956?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=398&q=80" alt="avatar">
<p class="text-gray-600 text-sm mx-2">
<span class="font-bold" href="#">Abigail Bennett</span> start following you . 3h
</p>
</a>
</div>
<a href="#" class="block bg-gray-800 text-white text-center font-bold py-2">Marquer comme lu</a>
</div>
</div>
</div>
<div class="ml-3 relative">
<div x-data="{ dropdownOpen: false }" class="relative my-32">
<button @click="dropdownOpen = !dropdownOpen" class="relative z-10 block rounded-md p-2 focus:outline-none">
<span class="sr-only">Open user menu</span>
<img class="h-8 w-8 rounded-full" src="img/profile_user.svg" alt="">
</button>
<div x-show="dropdownOpen" @click="dropdownOpen = false" class="fixed inset-0 h-full w-full z-10" style="display: none;"></div>
<div x-show="dropdownOpen" class="absolute w-64 right-0 mt-2 bg-white rounded-md shadow-lg overflow-hidden z-20" style="display: none;">
<div class="py-2">
<a href="#" class="flex block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100" role="menuitem"><img class="mr-5" src="icons/outline/users.svg">Link 1</a>
<a href="#" class="flex block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100" role="menuitem"><img class="mr-5" src="icons/outline/user.svg">Link 2</a>
<a href="#" class="flex block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100" role="menuitem"><img class="mr-5" src="icons/outline/adjustments.svg">Link 3</a>
<a href="#" class="flex block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100" role="menuitem"><img class="mr-5" src="icons/outline/lightning-bolt.svg">Link 4</a>
<a href="#" class="flex block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100" role="menuitem"><img class="mr-5" src="icons/outline/support.svg">Link 5</a>
<a href="#" class="flex block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100" role="menuitem"><img class="mr-5" src="icons/outline/logout.svg">Link 6</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Mobile menu, show/hide based on menu state. -->
<div class="sm:hidden" id="mobile-menu">
<div class="px-2 pt-2 pb-3 space-y-1">
<!-- Current: "bg-gray-900 text-white", Default: "text-gray-300 hover:bg-gray-700 hover:text-white" -->
<a href="/recettes" class="text-gray-300 hover:bg-gray-700 hover:text-white block px-3 py-2 rounded-md text-base font-medium">Recettes</a>
<a href="/planning" class="bg-gray-900 text-white block px-3 py-2 rounded-md text-base font-medium">Planning</a>
<a href="/courses" class="text-gray-300 hover:bg-gray-700 hover:text-white block px-3 py-2 rounded-md text-base font-medium">Courses</a>
<a href="/mensuration" class="text-gray-300 hover:bg-gray-700 hover:text-white block px-3 py-2 rounded-md text-base font-medium">Mensuration</a>
</div>
</div>
</nav>
如何使用 jQuery 或/和 Alpine 使其正常工作? (我想要两种解决方案)
我面临的一个问题,在 sn-p 中可以同时打开两个下拉菜单
【问题讨论】:
标签: jquery css tailwind-css alpine.js