0
0
Svelteframework~20 mins

Custom transitions in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 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.