youtube API上的Javascript电子邮件选择表单事件

Javascript email opt-in form event on youtube API

本文关键字:选择 表单 事件 电子邮件 Javascript API 上的 youtube      更新时间:2023-09-26

我正在使用youtube API调用灯箱内的视频。我试图实现的是在视频结束后添加一个电子邮件选择加入表格。我已经设法在视频结束后立即调用电子邮件表单,但它仍在灯箱/视频后面。如何将联系人表格放在灯箱/视频的前面?

<!DOCTYPE html>
<html>
<head>      
<script src="./js/jquery.js"></script>
 <script src="http://www.youtube.com/player_api"></script>
<style>
.fancybox-nav {
width: 60px;       
}
.fancybox-nav span {
visibility: visible;
}
.fancybox-next {
right: -60px;
}
.fancybox-prev {
left: -60px;
}

#overlayContactForm {
 visibility: hidden;
 position: absolute;
 left: 0px;
 top: 0px;
 width:100%;
 height:100%;
 text-align:center;
 z-index: 1000;
}


 #overlayContactForm div {
 width: 428px;
 margin: 65px;
 background-color: #fff;
 border: 1px solid #000;
 padding: 15px;
 text-align: center;
 height: 200px;

 }
 </style>
</head>
<body>
<div id="player"></div>
<script src="http://www.youtube.com/player_api"></script>
<div align="center">

 <script>
  // Fires whenever a player has finished loading
  function onPlayerReady(event) {
event.target.playVideo();
}
// Fires when the player's state changes.
function onPlayerStateChange(event) {
// Go to the next video after the current one is finished playing
if (event.data === 0) {
   // $.fancybox.next();
 // Call hidden form    
el = document.getElementById("overlayContactForm");
el.style.visibility = (el.style.visibility == "visible") ? "hidden" : 
"visible";
}
}
 // The API will call this function when the page has finished downloading 
 the JavaScript for the player API
 function onYouTubePlayerAPIReady() {
// Initialise the fancyBox after the DOM is loaded
$(document).ready(function() {
    $(".fancybox")
        .attr('rel', 'gallery')
        .fancybox({
            openEffect  : 'none',
            closeEffect : 'none',
            nextEffect  : 'none',
            prevEffect  : 'none',
            padding     : 0,
            margin      : 50,
            beforeShow  : function() {
                // Find the iframe ID
                var id = $.fancybox.inner.find('iframe').attr('id');
                // Create video player object and add event listeners

            }
        });
});
}     

 </script>

 <!-- This loads the YouTube IFrame Player API code -->
 <script src="http://www.youtube.com/player_api"></script>
 <a class="fancybox fancybox.iframe" href="http://www.youtube.com/embed    
 /L9szn1QQfas?enablejsapi=1&wmode=opaque">Video #1</a>
 <br />
  <a class="fancybox fancybox.iframe" href="http://www.youtube.com/embed
  /cYplvwBvGA4?enablejsapi=1&wmode=opaque">Video #2</a>

  <div id="overlayContactForm">
  <div>
  <h1> Testing</h1>
  <form class="form-inline">
  <input type="email" class="form-control" id="exampleInputEmail3" 
  placeholder="Email Address">
  <button type="submit" class="btn btn-default">Sign in</button>
  </form>
  <p> Testing email opt-in - Video/Form </p>
  </div>
  </div>
  </body>
  </html>

要回答您的问题:"如何将联系人表单放在lightbox/video的前面?"您需要增加表单的css z-index属性。下面是一种方法。计算出页面上当前最高的z索引是多少,然后递增该值。Flash有一个名为"getNextHighestDepth"的方法。我们可以做类似的事情!警告:如果你的网页HTML太重,这种技术可能会很慢。

function Loops(collection, fn) {
  /**
   * @method generic reuseable loop with callback for specific actions
   * @param {Array|NodeList|HTMLCollection|etc.} collection
   * @param {function} fn - @callback
   */
  'use strict';
  var i;
  if ((collection.item && collection.length) || collection instanceof Array || collection instanceof Element || collection.elements || collection.jquery) {
    i = collection.length || collection.childNodes.length;
    if (i > -1) {
      do {
        if (collection[i] !== undefined) {
          fn(i);
        }
      } while (--i >= 0);
    }
  } else {
    throw new Error('"collection" (' + collection + ') is not valid. It should be a single element or an array or have an "item" method and a "length" property. Form controls collections are also valid');
  }
};
var GetNextHighestDepth = function(incrementBy) {
  /**
   * @returns {number} highest
   * @method gets highest z-index and adds 10
   * or adds number specified in parameter
   * @todo make return value 'live'. zindex returns a static value;
   */
  'use strict';
  var items = document.getElementsByTagName('*'),
    highest = 0;
  Loops(items, function(i) {
    // zindex returns a string that must be converted to a number
    var zindex = getComputedStyle(items[i], null).getPropertyValue('z-index');
    if ((zindex > highest) && (zindex !== 'auto')) {
      if (typeof incrementBy === 'number') {
        highest = parseInt(zindex, 10) + incrementBy;
      } else {
        highest = parseInt(zindex, 10) + 10;
      }
    }
  });
  return highest;
};

// Fires whenever a player has finished loading
function onPlayerReady(event) {
  event.target.playVideo();
}
// Fires when the player's state changes.
function onPlayerStateChange(event) {
  // Go to the next video after the current one is finished playing
  if (event.data === 0) {
    //   $.fancybox.next();
    el = document.getElementById("overlayContactForm");
    el.style.zIndex = GetNextHighestDepth();
    el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
  }
}
// The API will call this function when the page has finished downloading the JavaScript for the player API
function onYouTubePlayerAPIReady() {
  // Initialise the fancyBox after the DOM is loaded
  $(document).ready(function() {
    $(".fancybox")
      .attr('rel', 'gallery')
      .fancybox({
        openEffect: 'none',
        closeEffect: 'none',
        nextEffect: 'none',
        prevEffect: 'none',
        padding: 0,
        margin: 50,
        beforeShow: function() {
          // Find the iframe ID
          var id = $.fancybox.inner.find('iframe').attr('id');
          // Create video player object and add event listeners
          var player = new YT.Player(id, {
            events: {
              'onReady': onPlayerReady,
              'onStateChange': onPlayerStateChange
            }
          });
        }
      });
  });
}