youtube API上的Javascript电子邮件选择表单事件
Javascript email opt-in form event on youtube API
我正在使用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
}
});
}
});
});
}
相关文章:
- 在带有 bootbox 的自定义对话框中添加选项以选择表单控件.js在 ASP.NET MVC 中
- 如何通过按钮更改选择表单
- 在提交数据之前更改选择表单元素中的值
- 试图让Jquery在Rails和ActiveAdmin中使用动态选择表单
- HTML使用选择表单更改输入选择(可能使用JQuery?)
- Mootools提交表单仅在通过ID选择表单时有效
- 如何发布单选和复选框选择?表单由php和js处理
- 订阅选择表单中的不同选项
- 带有选择表单和文本字段的简单计算器
- text()不适用于选择表单输入
- 按下按键输入时输入表单和选择表单之间的冲突
- 选择表单后从数据库中获取隐藏的输入值
- 如何在给定焦点时选择表单输入中的文本
- 触发链接以在 If 语句中选择表单时触发
- 使用 Javascript 函数从 HTML 选择表单生成价格
- 使用 JavaScript 清空选择表单元素
- Ajax 动态选择表单未提交值
- JavaScript 选择表单选项
- 创建元素时不要更改选择表单的选项
- 如何做一个HTML选择表单,其中所选选项更新JSON值