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

Faceted Search with Stimulus and Turbo Frames
10 December 2024

Harness the power of Stimulus and Turbo Frames to drive a simple but powerful faceted search solution

Turbo Frames - Markdown Preview
08 October 2024

Typeahead previews of markdown by simple Turbo Frame rerendering

Turbo Frames - Render Flash Messages Upon Form Submission
27 August 2024

Intercept form submission responses to render flash messages that are outside the originating Turbo Frame

Cookies
essential