简体   繁体   English

如何更改单击或自动播放视频的背景颜色和文本颜色

[英]How to change background color and text color of clicked or auto-played video

Problem: I have a HTML video playlist of Wistia videos and I'm trying to change the background-color and color of the videos as they are played, either by clicking on them or having the playlist autoplay. 问题:我有一个Wistia视频的HTML视频播放列表,我试图通过点击它们或播放列表自动播放来更改视频的背景颜色和颜色。

I'm currently using a:focus to accomplish the click part, but I'm looking for something more sophisticated? 我目前正在使用:专注于完成点击部分,但我正在寻找更复杂的东西? as a solution. 作为解决方案。 If you click out of the video playlist or on the video itself, the colors go back to their defaults. 如果您单击视频播放列表或视频本身,颜色将恢复为默认值。

Here is my code so far: https://codepen.io/atnd/pen/qzaVLX 这是我到目前为止的代码: https//codepen.io/atnd/pen/qzaVLX

JavaScript is not my strong suit, so I don't really know where to begin. JavaScript不是我的强项,所以我真的不知道从哪里开始。

I'm using Embed Links to build a playlist, but I referenced Wistia's API and am unsure what is applicable: https://wistia.com/support/developers/player-api && https://wistia.com/support/developers/playlist-api 我正在使用Embed Links来构建播放列表,但我引用了Wistia的API并且不确定适用的是什么: https ://wistia.com/support/developers/player-api&& https://wistia.com/support/developers /播放列表的API

Here's my pathetic JS: 这是我可怜的JS:

var vidcontainer = document.getElementById("vidcontainer");
var videos = video.getElementsByTagName("a");

for(i=0; i<videos.length; i++) {
  videos[i].addEventListener("click", function(event) {

  });
}

Thank you in advance for any help you can provide! 提前感谢您提供的任何帮助!

You could use something like this for toggling the backgroundColor when the menu item is clicked : 单击菜单项时,可以使用类似的方法切换backgroundColor:

var vidcontainer = document.getElementById("vidcontainer");
var videos = vidcontainer.getElementsByTagName("a");

function highlightMenuItemAndDisableOthers(element) {
  element.style.backgroundColor = 'red'
  for (let video of videos) {
    if (video !== element) video.style.backgroundColor = 'white'
  }
}

for (let video of videos) {
  video.addEventListener('click', event => {
    highlightMenuItemAndDisableOthers(video, videos)
  })
}

For knowing which video is played automatically by the player, it will be more complex. 要知道播放器自动播放哪个视频,它会更复杂。 You have to find an event in the API that tell when a new video is playing, then get the DOM element of the menu item and call the highlightMenuItemAndDisableOther method on it. 您必须在API中找到一个事件,告诉您何时播放新视频,然后获取菜单项的DOM元素并在其上调用highlightMenuItemAndDisableOther方法。

Hope it helps! 希望能帮助到你!

I went ahead and wrote a pure js solution for you. 我继续为你写了一个纯粹的js解决方案。 You want to contain your class toggling somehow. 你想以某种方式包含你的课程。 Here is an example of the switching logic and a link to your working script on the bottom. 以下是切换逻辑的示例以及底部工作脚本的链接。

 var videos = document.getElementsByTagName('a'); for(i=0; i<videos.length; i++) { videos[i].addEventListener("click", function(event) { document.querySelectorAll('li').forEach( el => el.classList.remove('active') ); event.target.parentNode.classList.add('active'); }); } 
 .active { font-weight: bold; } 
 <nav id="Nav" class="interest-nav"> <ul id="interest-ul"> <li><a href="#box1">Fashion</a></li> <li><a href="#box2">Movies</a></li> <li><a href="#box3">TV</a></li> </ul> </nav> 

Here is your solution: https://codepen.io/jacobshenning/pen/pXNOay 这是您的解决方案: https//codepen.io/jacobshenning/pen/pXNOay

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM