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 - Swiper with Autoplay and View Transitions
14 January 2025

Create an autoplaying swiper using view transitions and Turbo Frames

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

Cookies
essential