gpt4 book ai didi

javascript - 使用 vue.js 在 tailwind ui navbar 组件中修复下拉自动打开

转载 作者:行者123 更新时间:2023-12-03 06:47:35 25 4
gpt4 key购买 nike

我用 navbar component从顺风用户界面。它看起来像这样:

<!-- Profile dropdown -->
<div class="ml-3 relative">
<div>
<button
class="flex text-sm border-2 border-transparent rounded-full focus:outline-none focus:border-white transition duration-150 ease-in-out"
id="user-menu"
aria-label="User menu"
aria-haspopup="true"
>
<img
class="h-8 w-8 rounded-full"
src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80"
alt
/>
</button>
</div>
<!--
Profile dropdown panel, show/hide based on dropdown state.

Entering: "transition ease-out duration-100"
From: "transform opacity-0 scale-95"
To: "transform opacity-100 scale-100"
Leaving: "transition ease-in duration-75"
From: "transform opacity-100 scale-100"
To: "transform opacity-0 scale-95"
-->
<div class="origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg">
<div
class="py-1 rounded-md bg-white shadow-xs"
role="menu"
aria-orientation="vertical"
aria-labelledby="user-menu"
>
<a
href="#"
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem"
>Your Profile</a>
<a
href="#"
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem"
>Settings</a>
<a
href="#"
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem"
>Sign out</a>
</div>
</div>
</div>
在这种情况下,当我在 vue.js 中运行此代码时,导航栏下拉菜单状态默认为打开。如何设置默认关闭状态?
这是预览:
enter image description here

最佳答案

我不确定现在是否有人在关注这个问题,但我正在分享我的解决方案。
在下拉代码的片段中有一条评论说:

            <!--
Profile dropdown panel, show/hide based on dropdown state.

Entering: "transition ease-out duration-100"
From: "transform opacity-0 scale-95"
To: "transform opacity-100 scale-100"
Leaving: "transition ease-in duration-75"
From: "transform opacity-100 scale-100"
To: "transform opacity-0 scale-95"
-->

它基本上告诉下拉的状态正在根据类名发生变化,所以你必须让它们像这样动态:

<div :class="`origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg transition ease-${dropdown ? 'out' : 'in'} duration-${dropdown ? '100' : '75'} transform opacity-${dropdown ? '100' : '0'} scale-${dropdown ? '100' : '95'}`">

现在这些类将取决于下拉值,它只是组件的一个属性,可以通过如下点击事件进行更改:

export default {
name: 'TheNavBar',
data() {
return {
dropdown: false,
}
},
}
<div>
<button
id="user-menu"
class="flex text-sm border-2 border-transparent rounded-full focus:outline-none focus:border-white transition duration-150 ease-in-out"
aria-label="User menu"
aria-haspopup="true"
@click="dropdown = !dropdown"
>
</button>
</div>

关于javascript - 使用 vue.js 在 tailwind ui navbar 组件中修复下拉自动打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62768011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com