基于条件类更改的 CSS 转换在使用 vuejs 和 tailwind 时不起作用
P粉872182023
P粉872182023 2023-08-30 10:21:09
[Vue.js讨论组]
<p>我正在尝试使用 <strong>tailwind</strong> <code>transition</code> 类来展开侧边栏 <strong>Vuejs 内的 <code><router-link></code> 子菜单</strong> 组件。</p> <pre class="brush:php;toolbar:false;">// template &lt;div :class=&quot;`${index === 0 ? 'mt-2' : ''} sidebar-link sidebar-dropdown-link`&quot; @click=&quot;link.open = !link.open&quot; &gt; &lt;div class=&quot;flex items-center&quot;&gt; &lt;FontAwesomeIcon :icon=&quot;link.icon&quot; class=&quot;sidebar-link-icon&quot; /&gt; {{ link.label }} &lt;/div&gt; &lt;FontAwesomeIcon :icon=&quot;link.open ? ['fas', 'chevron-up'] : ['fas', 'chevron-down']&quot; class=&quot;sidebar-dropdown-link-icon&quot; /&gt; &lt;/div&gt; // This is the funny line... &lt;div :class=&quot;`${link.open ? `h-[${link.sublinks.length * 45}px]` : 'h-0'} sidebar-dropdown-menu`&quot;&gt; &lt;router-link v-for=&quot;(sublink, index) in link.sublinks&quot; :key=&quot;index&quot; class=&quot;sidebar-link mt-1&quot; :to=&quot;sublink.route&quot; &gt; &lt;FontAwesomeIcon :icon=&quot;sublink.icon&quot; class=&quot;sidebar-link-icon pl-3&quot; /&gt; {{ sublink.label }} &lt;/router-link&gt; &lt;/div&gt;</pre> <p>预期的行为是,当<code>link.open</code>变为<code>true</code>时,<strong>有趣的行<中定义的<code><div></code>标签/strong> 以 300 毫秒的过渡展开(或显示),其 <code>height</code> 值已根据 <code>link.sublinks.length</code> 值定义,所有这些都应用 <code> sidebar-dropdown-menu</code> CSS 类。我的意思是:</p> <pre class="brush:php;toolbar:false;">.sidebar-dropdown-menu { @apply w-full overflow-hidden transition-[height] duration-[300ms] }</pre> <p>如果 <code>link.sublinks.length</code> 等于 <code>3</code>,则应用的自定义类将为 <code>h-[135px]</code> 并且 <code> <strong>funny line</strong> 中的 <div></code> 标记出现,但是尽管 <code>p-0</code> 类按预期工作,但转换不起作用。 Google Chrome 开发工具向我显示 <code>height: 135px</code> 作为禁用的 CSS 规则。</p> <p>最后,如果我将 <code>h-[${link.sublinks.length * 45}px]</code> 替换为常量值(例如 <code>h-[200px]</code>)一切都按预期进行。</p> <p>我怀疑 <code>h-auto</code> 正在覆盖我的身高自定义类。</p> <p>我是拉丁人,所以我的英语不太好看。</p> <p>提前致谢。</p>
P粉872182023
P粉872182023

全部回复(1)
P粉211273535

我已经实现了它的工作,但我不喜欢我找到的解决方案:

<div
    :style="`${link.open ? `height: ${link.sublinks.length * 45}px` : 'height: 0px'}`"
    class="sidebar-dropdown-menu"
>

只是避免使用 Tailwind 类来定义高度属性,现在就是这样。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号