You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Alpine.js的x-transitions转换为Vue.js的过渡类?

Fixing Vue Transition Not Working When Porting from Alpine.js

Hey there! No worries at all—transition syntax differences between Alpine.js and Vue are super easy to mix up when you're switching frameworks, especially when you're still getting up to speed with front-end transitions. Let's break down what's going wrong and get your animation working.

The Core Issue: Misaligned Transition Class Mapping

The problem comes down to how Vue and Alpine.js name their transition state classes—they don't map 1:1 the way you might expect. Here's the correct correspondence between your Alpine code and Vue's transition props:

Alpine.js DirectiveVue Transition PropPurpose
x-transition:enterenter-active-classDefines the base transition (timing, easing, duration) for the enter animation
x-transition:enter-startenter-classThe initial state of the element when entering (before the animation starts)
x-transition:enter-endenter-to-classThe final state of the element when entering (animation completes)
x-transition:leaveleave-active-classBase transition for the leave animation
x-transition:leave-startleave-classInitial state of the element when leaving (animation starts here)
x-transition:leave-endleave-to-classFinal state of the element when leaving (animation completes)

In your original Vue code, you swapped the roles of enter-class and enter-active-class, plus misassigned the leave classes too. That meant Vue wasn't applying the animation timing correctly, and the state classes were applied at the wrong stages—resulting in no visible transition.

The Corrected Vue Transition Code

Here's the fixed version that matches your Alpine.js animation exactly:

<transition
  enter-active-class="transition ease-out duration-100"
  enter-class="transform opacity-0 scale-95"
  enter-to-class="transform opacity-100 scale-100"
  leave-active-class="transition ease-in duration-75"
  leave-class="transform opacity-100 scale-100"
  leave-to-class="transform opacity-0 scale-95"
>
  <div v-if="open">
    <!-- Your content goes here -->
  </div>
</transition>

Quick Additional Checks

To make sure everything works smoothly:

  • Double-check that open is a reactive variable in your Vue component (e.g., declared with const open = ref(false) in Composition API, or data() { return { open: false } } in Options API). Without reactivity, Vue won't trigger the transition when open changes.
  • Confirm Tailwind CSS is properly set up in your Vue project—all the classes like transition, scale-95, and opacity-0 need to be available to the component.
  • Ensure the <div> inside the transition has content (empty divs might not show the transition clearly, especially scaling effects).

内容的提问来源于stack exchange,提问作者Micheal J. Roberts

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 19:42:35