Bird
Raised Fist0
Svelteframework~20 mins

Custom transitions in Svelte - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Svelte Custom Transitions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Svelte custom transition?
Consider this Svelte component using a custom fade transition. What will be the opacity of the element at half the transition duration?
Svelte
<script>
  import { cubicOut } from 'svelte/easing';

  function fade(node, { delay = 0, duration = 400 }) {
    return {
      delay,
      duration,
      css: t => `opacity: ${t}; transform: scale(${t});`,
      easing: cubicOut
    };
  }
</script>

<div transition:fade={{ duration: 400 }}>
  Hello!</div>
AOpacity is 0.5 and scale is 0.5 at half duration
BOpacity is 0 and scale is 0 at half duration
COpacity is about 0.875 and scale is about 0.875 at half duration
DOpacity is 1 and scale is 1 immediately at half duration
Attempts:
2 left
💡 Hint
The easing function cubicOut makes the transition faster at the start and slower at the end.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Svelte custom transition function?
Choose the option that correctly defines a custom transition function in Svelte that changes opacity from 0 to 1 over the given duration.
Afunction fade(node, { duration }) { return { duration, css: t => `opacity: ${t}` }; }
Bfunction fade(node, duration) { return { duration, css: t => `opacity: ${t}` }; }
Cfunction fade(node, { duration }) { return { duration, css: t => `opacity: 1 - t` }; }
Dfunction fade(node, { duration }) { return { css: t => `opacity: ${t}` }; }
Attempts:
2 left
💡 Hint
The transition function receives the node and an object with parameters.
🔧 Debug
advanced
2:00remaining
Why does this custom transition not animate the element?
This Svelte component uses a custom transition but the element appears instantly without animation. What is the cause?
Svelte
<script>
  function slide(node) {
    return {
      duration: 300,
      css: t => `transform: translateX(${t * 100}px);`
    };
  }
</script>

<div transition:slide>
  Slide me
</div>
AThe transition function is missing the easing property, so it does not animate.
BThe css function uses t from 0 to 1 but translateX should go from 100px to 0px, so the direction is reversed.
CThe transition function must return a tick function, not css.
DThe transition function must use 'in:' or 'out:' instead of 'transition:'.
Attempts:
2 left
💡 Hint
Check the range of t and how it affects translateX value.
❓ state_output
advanced
2:00remaining
What is the final style of the element after this custom transition completes?
Given this Svelte custom transition, what style will the element have after the transition finishes?
Svelte
<script>
  function grow(node, { duration = 500 }) {
    return {
      duration,
      css: t => `transform: scale(${t}); opacity: ${t}`
    };
  }
</script>

<div transition:grow={{ duration: 500 }}>
  Grow me
</div>
Atransform: scale(1); opacity: 1
Btransform: scale(0); opacity: 0
Ctransform: scale(0.5); opacity: 0.5
Dtransform: scale(1); opacity: 0
Attempts:
2 left
💡 Hint
At the end of the transition, t equals 1.
🧠 Conceptual
expert
2:00remaining
Which statement about Svelte custom transitions is true?
Select the correct statement about how custom transitions work in Svelte.
ASvelte applies custom transitions only on elements with the 'in:' directive, not 'transition:'.
BCustom transitions must always use the tick function to update styles frame by frame.
CThe transition function must return a Promise that resolves when the animation ends.
DThe css function receives a parameter t that goes from 0 to 1 during the transition and returns a CSS string applied to the element.
Attempts:
2 left
💡 Hint
Think about the role of the css function in custom transitions.

Practice

(1/5)
1. What is the main purpose of a custom transition in Svelte?
easy
A. To create unique animations when elements enter or leave the page
B. To fetch data from an API
C. To manage component state
D. To handle user input events

Solution

  1. Step 1: Understand what transitions do in Svelte

    Transitions animate elements as they appear or disappear from the DOM.
  2. Step 2: Identify the role of custom transitions

    Custom transitions let you define your own animation styles for these changes.
  3. Final Answer:

    To create unique animations when elements enter or leave the page -> Option A
  4. Quick Check:

    Custom transitions = unique enter/leave animations [OK]
Hint: Custom transitions animate element entry/exit smoothly [OK]
Common Mistakes:
  • Confusing transitions with data fetching
  • Thinking transitions manage state
  • Mixing transitions with event handling
2. Which of the following is the correct way to define a custom transition function in Svelte?
easy
A. function fade(node) { return { delay: 300, css: t => `opacity: ${t}` }; }
B. function fade(params) { return { duration: 300, css: t => `opacity: ${t}` }; }
C. function fade() { return { duration: 300, css: t => `opacity: ${t}` }; }
D. function fade(node, params) { return { duration: 300, css: t => `opacity: ${t}` }; }

Solution

  1. Step 1: Recall the custom transition function signature

    It must accept node and optionally params.
  2. Step 2: Check the returned object structure

    The object should include duration and a css function using t for progress.
  3. Final Answer:

    function fade(node, params) { return { duration: 300, css: t => `opacity: ${t}` }; } -> Option D
  4. Quick Check:

    Function(node, params) + {duration, css} = C [OK]
Hint: Custom transition needs node param and returns duration + css [OK]
Common Mistakes:
  • Omitting the node parameter
  • Returning delay instead of duration
  • Not using a css function with progress t
3. Given this custom transition function:
function slide(node) {
  return {
    duration: 400,
    css: t => `transform: translateX(${(1 - t) * 100}%)`
  };
}

What happens to the element when it enters the DOM using transition:slide?
medium
A. It fades in with increasing opacity
B. It slides out to the right when leaving
C. It slides in from the right to its original position
D. It instantly appears without animation

Solution

  1. Step 1: Analyze the css function for entry

    The transform moves from translateX(100%) at t=0 to translateX(0%) at t=1.
  2. Step 2: Understand the visual effect

    The element starts off-screen right (100%) and slides left to original place (0%).
  3. Final Answer:

    It slides in from the right to its original position -> Option C
  4. Quick Check:

    translateX from 100% to 0% = slide in right to left [OK]
Hint: Check transform values at t=0 and t=1 for motion direction [OK]
Common Mistakes:
  • Confusing enter and leave animations
  • Assuming opacity changes instead of transform
  • Thinking it slides out instead of in
4. This custom transition code causes an error:
function grow(node) {
  return {
    duration: 500,
    css: t => `scale(${t})`
  };
}

What is the error and how to fix it?
medium
A. Missing 'transform:' prefix in css string; fix by returning `transform: scale(${t})`
B. Duration must be a string; fix by changing 500 to '500ms'
C. Function must return a number, not a string; fix by removing css
D. Node parameter is unused; fix by removing it

Solution

  1. Step 1: Identify the CSS syntax error

    The css string lacks the CSS property name; 'scale' alone is invalid.
  2. Step 2: Correct the CSS string

    It should be 'transform: scale(...)' to apply scaling properly.
  3. Final Answer:

    Missing 'transform:' prefix in css string; fix by returning `transform: scale(${t})` -> Option A
  4. Quick Check:

    CSS property needed before scale() [OK]
Hint: CSS string must include property name like 'transform:' [OK]
Common Mistakes:
  • Returning only scale() without 'transform:'
  • Using wrong duration type
  • Removing needed node parameter
5. You want a custom transition that fades in and grows an element from half size to full size over 600ms. Which function correctly implements this?
hard
A. function fadeGrow(node) { return { duration: 600, css: t => `opacity: ${1 - t}; transform: scale(${t})` }; }
B. function fadeGrow(node) { return { duration: 600, css: t => `opacity: ${t}; transform: scale(${0.5 + 0.5 * t})` }; }
C. function fadeGrow(node) { return { duration: 600, css: t => `opacity: ${t}; transform: scale(${t / 2})` }; }
D. function fadeGrow(node) { return { duration: 600, css: t => `opacity: ${t}; transform: scale(${1 - t})` }; }

Solution

  1. Step 1: Understand the fade and grow requirements

    Opacity should go from 0 to 1 (fade in), scale from 0.5 to 1 (grow).
  2. Step 2: Check each option's css function

    function fadeGrow(node) { return { duration: 600, css: t => `opacity: ${t}; transform: scale(${0.5 + 0.5 * t})` }; } uses opacity = t (0 to 1) and scale = 0.5 + 0.5 * t (0.5 to 1), matching requirements.
  3. Final Answer:

    function fadeGrow(node) { return { duration: 600, css: t => `opacity: ${t}; transform: scale(${0.5 + 0.5 * t})` }; } -> Option B
  4. Quick Check:

    Opacity 0->1 and scale 0.5->1 = function fadeGrow(node) { return { duration: 600, css: t => `opacity: ${t}; transform: scale(${0.5 + 0.5 * t})` }; } [OK]
Hint: Map t from 0-1 to desired opacity and scale ranges [OK]
Common Mistakes:
  • Reversing opacity or scale ranges
  • Using scale starting at 0 instead of 0.5
  • Mixing up increasing and decreasing values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard