How to embed YouTube video in a webpage with a custom play-button, original poster at the best resolution and responsive container, keeping aspect ratio
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

15 lines
843B

  1. (function (document, window) {
  2. document.addEventListener("DOMContentLoaded", function (event) {
  3. const videos = document.querySelectorAll('.youtube-video .youtube-video__play-icon')
  4. videos.forEach(function (video) {
  5. video.addEventListener("click", function (e) {
  6. const link = e.target.dataset.link || null;
  7. const parent = e.target.closest('.youtube-video__wrapper');
  8. if (link && parent) {
  9. parent.classList.add('loading');
  10. parent.innerHTML = '<iframe class="youtube-video__iframe" src="' + link + '?autoplay=1" frameborder="0" allowfullscreen allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"></iframe>';
  11. }
  12. });
  13. })
  14. });
  15. }(document, window))