Scrolling Lyrics using Turbo Frames

Create a "scrolling lyrics" animation using Turbo Frames and View Transitions

View Solution on Patreon
Scrolling Lyrics using Turbo Frames

Premise

as weā€™ve already observed, Turbo Frames are ideal for exchanging content on the fly using their src attribute. In this challenge, weā€™ll look at how we can do that in a time-sensitive manner, for example as the time code of a video changes.

Starting Point

Our starting point contains a YouTube video in a <lite-youtube> container element:

https://stackblitz.com/edit/turbo-frames-scrolling-lyrics?file=index.html%3AL63

(yes youā€™ve just been RickRolled šŸ™ƒ)

It also contains a Turbo Frame with a placeholder for the lyrics:

https://stackblitz.com/edit/turbo-frames-scrolling-lyrics?file=index.html%3AL75

The server is equipped with an endpoint that responds to the /lyrics route with the lyrics for a given timecode in seconds when passed as the t param (e.g. /lyrics?t=10):

https://stackblitz.com/edit/turbo-frames-scrolling-lyrics?file=index.js%3AL20

If thereā€™s a match for the timecode in the lyrics object, it responds with an appropriate Turbo Frame, else with a 404 error.

Challenge

Your challenge is to implement a ā€œScrolling Lyricsā€ algorithm using the time updates sent by the player.

A word of warning: Because Iā€™m using a YouTube video which is embedded in an <iframe>, I have to do a little dance to listen for time updates from the YT Iframe Player API (hereā€™s a Codepen for context). For a regular <video> element, youā€™d just listen for the timeupdate event. But I thought it was funnier this way šŸ˜….

Now go ahead and

Hereā€™s a preview of the result:

You've been rickrolled!

Teaser

  • How would you add view transitions to the swapping of Turbo Frames for a smoother experience?

More from

Turbo Frames - Inline Edit
27 February 2024

Manage Inline Editing with Turbo Frames and Cookies

Turbo Frames - Typeahead Search
07 November 2023

Update filter results using eager loading Turbo Frames.

Turbo Frames - Lifecycle of a Lazy Loaded Frame
26 September 2023

Manage view state with Turbo Frames and Stimulus.

Cookies
essential