YouTube Video Gallery

In todays Custom Interaction experiment I’m going to show you how to create a custom YouTube gallery that will change the video when a video thumbnail is clicked.

Video Gallery

This entire setup uses a single Custom Interaction, the data() short code and a class.

The Video Bric
I’m just using the standard video Bric in this experiment, however, it’s important to note that I have applied an ID to the video iframe (my-video), this is done so that I can target the player with my Custom Interaction.

The Title
I have also applied an id (video-title) to the Header Bric.

Classes
Not much in the way of classes here, just a class I toggle on the currently selected video which applies a border.

The Interaction
This Interaction is pretty straightforward.

  1. The first rule will set the video title (#video-title) text using the data() short code which will be populated with the data attribute data-vid-title from the trigger element.
  2. The next rule will set the src attribute of the iframe (#my-video) video player using the data() short code which will be populated with the data attribute data-vid-url from the trigger element.
  3. This rule removes the selected-vid class from all of the thumbnails using their ids.
  4. Finally we apply the selected-vid class to the trigger element using the data() short code which will be populated with the data attribute data-item from the trigger element in order to target it.

The Thumbnails
Finally I setup the video thumbnail images. Each thumbnail has a unique ID which is used in conjunction with step 3 from above. :point_up_2:

Next I have set each image to trigger my custom interaction when the image is clicked.

And I have added all of the required data attributes on the image

  1. data-vid-url = Populated with the YouTube video url.
  2. data-vid-title = Populated with the video title.
  3. data-item = Populated with the image unique id.

Remember the values for these data attributes will change for each thumbnail.

That’s it. :sweat_smile:

I now I have a custom built YouTube gallery that uses a single video Bric to play multiple videos thanks to a single Custom Interaction and the data() short code. :fire:

You can download the experiment below and have a play around.

Youtube Video Gallery.bloc (225.3 KB)

Have fun and a great weekend! :beers:

3 Likes

Thanks, Norm! This will be super useful for my new Blocs Master website!

3 Likes

new-blocs-master

:ok_hand:

4 Likes

It’s good to see Custom Interactions are helping to make Blocs Master better. :sunglasses:

1 Like

@Norm,
It works perfect with Youtube videos.
It would even better if it could work with local videos.
I’ve tried a few things to achieve that but failed.
Any suggestions?

1 Like