在悬停/鼠标悬停时启动 youtube 视频 [英] Start youtube video on hover/mouseover

查看:24
本文介绍了在悬停/鼠标悬停时启动 youtube 视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 youtube 视频在悬停时开始.当用户将鼠标悬停在另一个视频上时,它会暂停(而不是停止)...

I'm trying to get youtube videos to start on hover. It will pause (not stop) when the user hovers over another video...

我被悬停命令困住了.有人可以帮我解决吗?

I am stuck on the hover command. Can someone help me work it out please?

该页面有 16 个视频,这是 jsfiddle 的工作代码,其中包含 3 个视频作为示例.

The page has 16 videos, this is the working code from the jsfiddle that contains 3 videos as an example.

http://jsfiddle.net/sebwhite/gpJN4/

视频:

<iframe id="player" width="385" height="230" src="http://www.youtube.com/embed/erDxb4IkgjM?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;controls=0" frameborder="0" allowfullscreen></iframe>

JAVASCRIPT:

JAVASCRIPT:

function onYouTubeIframeAPIReady() {
 player = new YT.Player('player', {
     events: {
         'onStateChange': onPlayerStateChange
     }
 });

onYouTubeIframeAPIReady();
function onPlayerStateChange(event) {
 if (event.data == YT.PlayerState.PLAYING) {
     player1.pauseVideo();
     player2.pauseVideo();
 }

推荐答案

更新的小提琴

试试这个:

 var $$ = function(tagname) { return document.getElementsByTagName(tagname); }
    
 function onYouTubeIframeAPIReady() {
     var videos = $$('iframe'), // the iframes elements
         players = [], // an array where we stock each videos youtube instances class
         playingID = null; // stock the current playing video
     for (var i = 0; i < videos.length; i++) // for each iframes
     {
         var currentIframeID = videos[i].id; // we get the iframe ID
         players[currentIframeID] = new YT.Player(currentIframeID); // we stock in the array the instance
         // note, the key of each array element will be the iframe ID
         
         videos[i].onmouseover = function(e) { // assigning a callback for this event
             if (playingID !== currentHoveredElement.id) {
               players[playingID].stopVideo();
             }
             var currentHoveredElement = e.target;
             if (playingID) // if a video is currently played
             {
                 players[playingID].pauseVideo();
             }
             players[currentHoveredElement.id].playVideo();
             playingID = currentHoveredElement.id;
         };
     }
    
 }
 onYouTubeIframeAPIReady();

小提琴:

http://jsfiddle.net/gpJN4/3/

这篇关于在悬停/鼠标悬停时启动 youtube 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆