0

I have several videos on my site, they are listed like the following :

<ul id="toolsButtons" class="ahrefs__tools__nav">
  <li>
    <div class="tools-icon">
      <a href=https://www.youtube.com/watch?v=A1nRiiWYgZw target=_blank><img src=Traditional_vs_GATE_s.PNG></a>
      <use xlink:href="#keyword-research-icon"></use>
      <p class="tools-icon__text">Traditional vs GATE</p>
  </li>
  <li>
    <div class="tools-icon">
      <a href=https://www.youtube.com/watch?v=wLHfGQlLqtE target=_blank><img src=Guess_GATE_Password.PNG></a>
      <use xlink:href="#competitive-analysis-icon"></use>
      <p class="tools-icon__text">GATE Passcode</p>
    </div>
  </li>
  <li>
    <div class="tools-icon">
      <a href=https://www.youtube.com/watch?v=5tAGemIvUeI target=_blank><img src=GATE_Demo.PNG></a>
      <use xlink:href="#keyword-research-icon"></use>
      <p class="tools-icon__text">How GATE Works</p>
    </div>
  </li>
</ul>

How can I create a viewing area below the list, so that when a video link is clicked, it will play in the viewing area?

Frank
  • 30,590
  • 58
  • 161
  • 244
  • as a quick thinking, you may create a hidden `div` containing a `video` tag and place it on top of the page and when a video is clicked you get its `src` attribute and use it to populate the video in the hidden `div`. – ThS Aug 23 '18 at 00:53
  • @ths, good suggestion, any sample code I can try ? – Frank Aug 23 '18 at 04:13
  • the method I suggested may look like [this](http://www.cogentstudios.com/wp-content/uploads/2015/10/modal-lightbox-squarespace-529x270.jpg) a blurred division that covers the page in which you put the video. – ThS Aug 23 '18 at 04:18
  • @ths, is there some code online that works so I can try it ? – Frank Aug 23 '18 at 14:55
  • if the method I suggested is fine to be used for you I can post it as answer. – ThS Aug 23 '18 at 14:56
  • @ths, yes, this seems doable, please post the code and I'll try it ^_^ ! – Frank Aug 23 '18 at 15:27
  • I may take some time cause I'm busy right now, hope you understand. – ThS Aug 23 '18 at 15:44
  • check out my answer. – ThS Aug 23 '18 at 19:47

1 Answers1

1

As I suggested in the comments, I created a div, initially hidden, that plays the role of a popup modal that will be showed when an a tag that has a data-target="modal" is clicked. I made some changes to your HTML code that I will cover them in the answer.

The process:

When you click a link that has data-target="modal" (a link that has an href attribute of a video, here I added a data-target="modal" to each one just to differentiate these links from the other links that may be present in the page) the modal div that i gave it id="modal" will be showed and filled with the appropriete video. So, each time a link is clicked its href attribute will be placed in the src attribute of the iframe tag that will host the video.

The final result:

Ps: I'll provide a runnable snippet, but, it may not work as intended due to some restrictions made by Stack Overflow. For that, I made a working DEMO on CodePen that you can check and play with the code, if you want.

var modal = document.getElementById('modal'), closeBtn = modal.querySelector('.close'), ytVideo = modal.querySelector('.content .yt-video'), title = modal.querySelector('.content .title'), anchors = document.querySelectorAll('a[data-target="modal"]'), l = anchors.length;
  for (var i = 0; i < l; i++) {
   anchors[i].addEventListener("click", function(e) {
    e.preventDefault();
    title.textContent = this.dataset.videoTitle || 'No title';
    ytVideo.src = this.href;
    modal.classList.toggle('is-visible');
    modal.focus();
   });
  }
  modal.addEventListener("keydown", function(e) {
   if (e.keyCode == 27) {
    title.textContent = '';
    ytVideo.src = '';
    this.classList.toggle('is-visible');
   }
  });
  closeBtn.addEventListener("click", function(e) {
   e.preventDefault();
   title.textContent = '';
   ytVideo.src = '';
   modal.classList.toggle('is-visible');
  });
#modal {
   display: none;
   position: fixed;
   width: 100vw;
   height: 100vh;
   max-height: 100vh;
   top: 0;
   left: 0;
   background: rgba(24, 24, 24, .6);
   z-index: 999;
  }
  #modal .content {
   width: 55%;
   height: 65vh;
   margin: auto; /* allows horyzontal and vertical alignment as .content is in flex container */
  }
  #modal .content .yt-video {
   display: block;
   width: 100%;
   height: calc(100% - 45px);
  }
  #modal .content .title {
   box-sizing: border-box;
   height: 45px;
   line-height: 23px;
   padding: 12px 4px;
   margin: 0;
   background: #007bff;
   max-width: 100%;
   white-space: nowrap;
   overflow: hidden;
   text-overflow: ellipsis;
  }
  #modal .close {
   position: absolute;
   top: 0;
   right: 0;
   width: 35px;
   height: 35px;
   line-height: 35px;
   text-align: center;
   border: 0;
   font-weight: bold;
   font-size: 24px;
   color: #fff;
   background: #666;
   cursor: pointer;
   transition: background .4s;
  }
  #modal .close:hover, #modal .close:active {
   background: #ef3658;
  }
  #modal.is-visible {
   display: flex;
  }
<ul id="toolsButtons" class="ahrefs__tools__nav">
   <li>
     <div class="tools-icon">
       <a href="https://www.youtube.com/embed/A1nRiiWYgZw" target="_blank" data-target="modal" data-video-title="Traditional vs GATE"><img src="Traditional_vs_GATE_s.png"></a>
       <use xlink:href="#keyword-research-icon"></use>
       <p class="tools-icon__text">Traditional vs GATE</p>
   </li>
   <li>
     <div class="tools-icon">
       <a href="https://www.youtube.com/embed/wLHfGQlLqtE" target="_blank" data-target="modal" data-video-title="GATE Passcode"><img src="Guess_GATE_Password.png"></a>
       <use xlink:href="#competitive-analysis-icon"></use>
       <p class="tools-icon__text">GATE Passcode</p>
     </div>
   </li>
   <li>
     <div class="tools-icon">
       <a href="https://www.youtube.com/embed/5tAGemIvUeI" target="_blank" data-target="modal" data-video-title="How GATE Works"><img src="GATE_Demo.png"></a>
       <use xlink:href="#keyword-research-icon"></use>
       <p class="tools-icon__text">How GATE Works</p>
     </div>
   </li>
 </ul>
  <!-- the modal div that will open when an anchor link is clicked to show the related video in an iframe. -->

 <div id="modal" tabindex="-1">
  <button type="button" data-dismiss="modal" class="close" title="close">&times;</button>
  <div class="content">
   <h4 class="title"></h4>
   <iframe class="yt-video" src="https://www.youtube.com/embed/A1nRiiWYgZw" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  </div>
 </div>

Changes that must be done in the HTML code:

  • Each a tag that holds an href attribute for a video that you want it to be showed in the modal must have a data-target attribute equals to "modal".
  • To display a title above the video when the modal div is showed, add a data-video-title attribute that equals to some text on these a tags. If the data-video-title is not present on a link, the title that appears above the video will be "No title".
  • As an illustration to the last 2 points, here's an example of a link that will open the modal when clicked: <a href="https://www.youtube.com/embed/A1nRiiWYgZw" target="_blank" data-target="modal" data-video-title="Traditional vs GATE"><img src="Traditional_vs_GATE_s.png"></a>
  • All the videos links (in other words the href attribute of the a tags) MUST be an embed link (https://www.youtube.com/embed/A1nRiiWYgZw as an example). So, you have to use the link in the src of the iframe under the embed section under share section on YouTube. Here are some images to demonstrate that:

FIrst Step Second Step Third Step

Why using embed instead of the regular watch link ?

That to prevent Cross-Domain-Policy restrictions. In simple words, by default servers don't allow other servers/sites to fetch their files asynchronously.

Notes on the modal div

  • For simplicity, I didn't done any animations as opening/closing effects, and the modal isn't responsive.
  • You can press ESC (Escape) button to close the modal.
  • A button with a "cross" sign will be placed on the top-right most of the browser window, to close the modal on click.
  • The markup of the modal div must be reserved to work as intended.

Why i didn't use data-href instead of href ?

I sticked with the href on the links to garantee that the video will be accessible even if JavaScript is disabled, if so, it will be redirected to the video.

Hope my answer pushed you further. I'm here for any further explanations.

ThS
  • 4,597
  • 2
  • 15
  • 27
  • I tried it, and it works properly, except one thing, while the video is playing, how do I close the playing window, there is no [x] on the upper right corner, I had to use the ESC key to get out, it should have a window close button on the upper right corner corner, also how to play it immediately without user need to click the play button ? – Frank Aug 23 '18 at 22:00
  • I tried to adapt your html into my file [ I changed it to a table listing videos ] , but it's not working : see my html code in my edited question. – Frank Aug 23 '18 at 22:24
  • you are not using embed links as I told you. And the close button is on the top right of the window with the letter X and when you hover it it becomes red. and you are using `a` tag that surrounds all of your code and that's really wrong try to change it with a `div`. Try to stick to all what I said in the answer. Visit the [demo link](https://codepen.io/Thabet_Sakhraoui/pen/mGeERb) and you'll get more familiar with what I said. – ThS Aug 23 '18 at 22:34
  • OK, I got them working mostly, except 2 of them, so why https://www.youtube.com/watch?v=ySqec8WrEQQ can play the video, but https://www.youtube.com/embed/ySqec8WrEQQ can not find the video ? – Frank Aug 24 '18 at 00:41
  • but it's working ! and in my demo everything is workking. I provided three screenshots on how to get the suitable link for the video. Please, try to follow all the steps. – ThS Aug 24 '18 at 01:01
  • Strange, I ftped the file to a test site [ http://gatecybertech.com/test ], now they are all working, but I still have the following 2 issues : [ 1 ] I can't find the [x] to close the pop-up window, try to see if you can see it. [ 2 ] How to specify to play a video from the time 29:30 in a long clip ? – Frank Aug 24 '18 at 01:22
  • put the modal `div` directly before the closing body tag and tell me what you'll get. – ThS Aug 24 '18 at 01:25
  • OK, now it's where you wanted it, but there is no more pop-up window, try it yourself : http://gatecybertech.com/test – Frank Aug 24 '18 at 02:27
  • put the javascript code just after the modal `div`. – ThS Aug 24 '18 at 02:40
  • you'll get something like this: div id=modal ..... script ..... – ThS Aug 24 '18 at 02:46
  • OK, done, now there is pop-up window, but still no close button. I figured out how to start and end at certain time. – Frank Aug 24 '18 at 02:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178653/discussion-between-ths-and-frank). – ThS Aug 24 '18 at 02:54