I searched for the perfect solution to show a custom high quality thumbnail for my embed videos. I did not want to use the more complex Youtube Javascript API solution. Here is how I archived the obstacle of using a custom thumbnail or Youtube maximum resolution thumbnail and a custom play button.
The following code uses jQuery but this could be done in Vanilla Javascript.
Technical explanation to use a custom Youtube thumbnail
First, I integrate my Youtube video with a custom
div
and custom thumbnail if needed. The data attribute tags are used to provide the Youtube video id and the custom thumbnail URL.Then, on page load, for each video on a page, the Javascript code creates a placeholder with the needed thumbnail (custom or Youtube maximum resolution one). A customized play button is added on top using CSS.
Finally, a click event listener is added to the placeholder to create an embed
iframe
when clicked. When showing theiframe
the placeholder is hidden.
The explanation is short and sweet, but take a look at the code and let me know what you think !
Youtube Custom Thumbnail Image: Cédric VT (@cedric_photography) from Unsplash
Cover Image: PJ Gal-Szabo (@pjgalszabo) from Unsplash
Hi thanks for this it works like a charm. I have a small problem where I cant play embedded video in full screen mode. Any idea how I can work around this?
Thanks again