Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Only the play button should be clickable

Currently, both the image and play button are both clickable.

How would I adjust it so that only the play button is clickable?

That is all I am trying to do in the code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am trying to figure out how to adjust it so that only the play button is clickable

How am I able to do that?

Is this able to be done?

code https://jsitor.com/3RCuY3s_O

(function(){
  let YouTubeContainers = document.querySelectorAll(".embed-youtube");

  // Iterate over every YouTube container you may have
  for (let i = 0; i < YouTubeContainers.length; i++) {
    let container = YouTubeContainers[i];
    let imageSource = "https://img.youtube.com/vi/"+ container.dataset.videoId +"/sddefault.jpg"; 

    // Load the Thumbnail Image asynchronously
    let image = new Image();
    image.src = imageSource;
    image.addEventListener("load", function() {
      container.appendChild(image);
    });

    // When the user clicks on the container, load the embedded YouTube video
    container.addEventListener("click", function() {
      let iframe = document.createElement( "iframe" );

      iframe.setAttribute("frameborder", "0");
      iframe.setAttribute("allowfullscreen", "");
      iframe.setAttribute("allow", "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture");
      // Important: add the autoplay GET parameter, otherwise the user would need to click over the YouTube video again to play it 
      iframe.setAttribute("src", "https://www.youtube.com/embed/"+ this.dataset.videoId +"?rel=0&showinfo=0&autoplay=1");

      // Clear Thumbnail and load the YouTube iframe
      this.innerHTML = "";
      this.appendChild( iframe );
    });
  }
})();
.container {
  position: absolute;
  left: 0;
  right: 0;
  min-height: 100%;
  display: flex;
  padding: 8px 8px;
}

.curtain {
  flex: 1 0 0;
  margin: auto;
  max-width: 640px;
  min-width: 155px;
  position: relative;
}

.embed-youtube {
  background-color: #000;
  margin-bottom: 10px;
  position: relative;
  height: 0;
  padding-top: 56.25%;
  overflow: hidden;
  
}

.embed-youtube img {
  width: 100%;
  top: -16.84%;
  left: 0;
  opacity: 0.7; 
}

/*.embed-youtube img,
.embed-youtube .embed-youtube-play {
  cursor: default;
}*/

.embed-youtube img,
.embed-youtube iframe,
.embed-youtube .embed-youtube-play,
.embed-youtube .embed-youtube-play:before {
  position: absolute;
}

.embed-youtube iframe {
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
}

 .embed-youtube .embed-youtube-play {
  -webkit-appearance: none;
  appearance: none;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  margin: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 90px;
  height: 90px;
  border-radius: 50%;
  cursor: pointer;
  border: 9px solid blue;
  background: transparent;
  filter: drop-shadow(3px 3px 3px #000000b3);
 
   z-index: 1;

}

.embed-youtube-play::before {
  content: "";
  width: 0;
  height: 0;
  border-top: 20px solid transparent;
  border-bottom: 20px solid transparent;
  border-left: 27px solid blue;
  transform: translateX(4px);
}

.embed-youtube-play:hover {
  box-shadow: 0 0 0 5px rgba(43, 179, 20, 0.5);
}

.embed-youtube-play:focus {
  outline: 0;
  box-shadow: 0 0 0 5px rgba(0, 255, 255, 0.5);
}
<div class="container">
   <div class="curtain">
     
<!-- 1. Video Wrapper Container -->
<div class="embed-youtube" data-video-id="djV11Xbc914">
  <!-- 2. The preview button that will contain the Play icon -->
  <button class="embed-youtube-play" type="button" aria-label="Open"></button>
</div>

<!-- 1. Video Wrapper Container -->
<div class="embed-youtube" data-video-id="djV11Xbc914">
  <!-- 2. The preview button that will contain the Play icon -->

  
     <button class="embed-youtube-play" type="button" aria-label="Open"></button>
</div>

<!-- 1. Video Wrapper Container -->
<div class="embed-youtube" data-video-id="djV11Xbc914">
  <!-- 2. The preview button that will contain the Play icon -->
 <button class="embed-youtube-play" type="button" aria-label="Open"></button>
</div>

<!-- 1. Video Wrapper Container -->
<div class="embed-youtube" data-video-id="djV11Xbc914">
  <!-- 2. The preview button that will contain the Play icon -->
  <button class="embed-youtube-play" type="button" aria-label="Open"></button>
</div>

<!-- 1. Video Wrapper Container -->
<div class="embed-youtube" data-video-id="djV11Xbc914">
  <!-- 2. The preview button that will contain the Play icon -->
  <button class="embed-youtube-play" type="button" aria-label="Open"></button>
</div>

</div>
</div>

>Solution :

You set the click event to the entire container. Set it to only the button (container.querySelector("button")), and change this to container to accomodate for the changes:

(function(){
  let YouTubeContainers = document.querySelectorAll(".embed-youtube");

  // Iterate over every YouTube container you may have
  for (let i = 0; i < YouTubeContainers.length; i++) {
    let container = YouTubeContainers[i];
    let imageSource = "https://img.youtube.com/vi/"+ container.dataset.videoId +"/sddefault.jpg"; 

    // Load the Thumbnail Image asynchronously
    let image = new Image();
    image.src = imageSource;
    image.addEventListener("load", function() {
      container.appendChild(image);
    });

    // When the user clicks on the container, load the embedded YouTube video
    container.querySelector("button").addEventListener("click", function() {
      let iframe = document.createElement( "iframe" );

      iframe.setAttribute("frameborder", "0");
      iframe.setAttribute("allowfullscreen", "");
      iframe.setAttribute("allow", "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture");
      // Important: add the autoplay GET parameter, otherwise the user would need to click over the YouTube video again to play it 
      iframe.setAttribute("src", "https://www.youtube.com/embed/"+ container.dataset.videoId +"?rel=0&showinfo=0&autoplay=1");

      // Clear Thumbnail and load the YouTube iframe
      container.innerHTML = "";
      container.appendChild( iframe );
    });
  }
})();
.container {
  position: absolute;
  left: 0;
  right: 0;
  min-height: 100%;
  display: flex;
  padding: 8px 8px;
}

.curtain {
  flex: 1 0 0;
  margin: auto;
  max-width: 640px;
  min-width: 155px;
  position: relative;
}

.embed-youtube {
  background-color: #000;
  margin-bottom: 10px;
  position: relative;
  height: 0;
  padding-top: 56.25%;
  overflow: hidden;
  
}

.embed-youtube img {
  width: 100%;
  top: -16.84%;
  left: 0;
  opacity: 0.7; 
}

/*.embed-youtube img,
.embed-youtube .embed-youtube-play {
  cursor: default;
}*/

.embed-youtube img,
.embed-youtube iframe,
.embed-youtube .embed-youtube-play,
.embed-youtube .embed-youtube-play:before {
  position: absolute;
}

.embed-youtube iframe {
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
}

 .embed-youtube .embed-youtube-play {
  -webkit-appearance: none;
  appearance: none;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  margin: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 90px;
  height: 90px;
  border-radius: 50%;
  cursor: pointer;
  border: 9px solid blue;
  background: transparent;
  filter: drop-shadow(3px 3px 3px #000000b3);
 
   z-index: 1;

}

.embed-youtube-play::before {
  content: "";
  width: 0;
  height: 0;
  border-top: 20px solid transparent;
  border-bottom: 20px solid transparent;
  border-left: 27px solid blue;
  transform: translateX(4px);
}

.embed-youtube-play:hover {
  box-shadow: 0 0 0 5px rgba(43, 179, 20, 0.5);
}

.embed-youtube-play:focus {
  outline: 0;
  box-shadow: 0 0 0 5px rgba(0, 255, 255, 0.5);
}
<div class="container">
  <div class="curtain">

    <!-- 1. Video Wrapper Container -->
    <div class="embed-youtube" data-video-id="djV11Xbc914">
      <!-- 2. The preview button that will contain the Play icon -->
      <button class="embed-youtube-play" type="button" aria-label="Open"></button>
    </div>

    <!-- 1. Video Wrapper Container -->
    <div class="embed-youtube" data-video-id="djV11Xbc914">
      <!-- 2. The preview button that will contain the Play icon -->


      <button class="embed-youtube-play" type="button" aria-label="Open"></button>
    </div>

    <!-- 1. Video Wrapper Container -->
    <div class="embed-youtube" data-video-id="djV11Xbc914">
      <!-- 2. The preview button that will contain the Play icon -->
      <button class="embed-youtube-play" type="button" aria-label="Open"></button>
    </div>

    <!-- 1. Video Wrapper Container -->
    <div class="embed-youtube" data-video-id="djV11Xbc914">
      <!-- 2. The preview button that will contain the Play icon -->
      <button class="embed-youtube-play" type="button" aria-label="Open"></button>
    </div>

    <!-- 1. Video Wrapper Container -->
    <div class="embed-youtube" data-video-id="djV11Xbc914">
      <!-- 2. The preview button that will contain the Play icon -->
      <button class="embed-youtube-play" type="button" aria-label="Open"></button>
    </div>

  </div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading