0Pricing
Vue Academy · Lesson

JavaScript Transition Hooks

@before-enter, @enter, @after-enter, @leave hooks, using GSAP for complex animations.

JavaScript Transition Hooks is a free Vue Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why JavaScript Hooks

CSS classes cover most cases, but some animations need JavaScript - integrating libraries like GSAP, animating canvas, or computing dynamic values. <Transition> exposes lifecycle hooks for exactly this.

The Available Hooks

Vue emits events for each phase: @before-enter, @enter, @after-enter, @enter-cancelled, and the matching leave hooks.

<Transition
  @before-enter="onBeforeEnter"
  @enter="onEnter"
  @after-enter="onAfterEnter"
  @leave="onLeave"
>
  <div v-if="show">Animated by JS</div>
</Transition>

before-enter Sets Initial State

@before-enter receives the DOM element and runs before insertion. Use it to set the starting styles, just like an enter-from class would.

function onBeforeEnter(el) {
  el.style.opacity = 0;
  el.style.transform = "translateY(20px)";
}

enter Receives el and done

@enter gets two arguments: the element and a done callback. You animate the element, then call done() when finished so Vue knows the transition ended.

function onEnter(el, done) {
  // animate to final state
  el.style.transition = "all 0.4s ease";
  el.style.opacity = 1;
  el.style.transform = "translateY(0)";
  el.addEventListener("transitionend", done, { once: true });
}

Always Call done

If you forget done(), Vue waits forever and the transition never completes - after-enter never fires and the element may stay in a half-animated state. Calling it is mandatory in JS-driven hooks.

function onEnter(el, done) {
  // ... animate ...
  setTimeout(done, 400); // fallback if no transitionend
}

leave with done

The @leave hook works the same way: animate the element out, then call done() so Vue removes it from the DOM.

function onLeave(el, done) {
  el.style.transition = "all 0.3s ease";
  el.style.opacity = 0;
  el.addEventListener("transitionend", done, { once: true });
}

after-enter for Cleanup

@after-enter runs once the enter finishes. Use it to remove inline styles, reset transition properties, or trigger follow-up logic.

function onAfterEnter(el) {
  el.style.transition = "";
  el.style.transform = "";
}

Skip CSS with :css="false"

When you animate purely in JS, set :css="false". This tells Vue to skip CSS class detection entirely, so it relies solely on your done callbacks for timing. It also avoids accidental CSS interference.

<Transition
  :css="false"
  @enter="onEnter"
  @leave="onLeave"
>
  <div v-if="show">Pure JS animation</div>
</Transition>

Integrating GSAP

JS hooks shine with animation libraries. GSAP can tween the element and accept done as its onComplete callback.

function onEnter(el, done) {
  gsap.from(el, {
    opacity: 0,
    y: 30,
    duration: 0.4,
    onComplete: done
  });
}

Cancelled Hooks

@enter-cancelled and @leave-cancelled fire if a transition is interrupted (e.g. the user toggles quickly). Use them to kill running tweens and avoid leftover state.

function onEnterCancelled(el) {
  gsap.killTweensOf(el);
}

When to Choose JS Hooks

Reach for JS hooks when you need physics, dynamic per-element values, library integration, or precise sequencing. For simple fades and slides, CSS classes stay simpler and more performant.

Quick Check

Why pass :css="false" with JavaScript hooks?

Recap

You learned the JS transition hooks: @before-enter (set initial state), @enter/@leave (animate, then call done), @after-enter (cleanup), plus :css="false" to bypass CSS classes and the cancelled hooks for interruptions.

Frequently asked questions

Is the “JavaScript Transition Hooks” lesson free?

Yes — the full text of “JavaScript Transition Hooks” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.

What will I learn in “JavaScript Transition Hooks”?

@before-enter, @enter, @after-enter, @leave hooks, using GSAP for complex animations. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vue Academy?

No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “JavaScript Transition Hooks” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vue Academy lesson?

Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Transition Component
  2. CSS Transitions and Animations
  3. JavaScript Transition Hooks
  4. TransitionGroup for List Animations
← Back to Vue Academy