Почему для показа видео YouTube требуется 2 клика?
Как исправить этот код, чтобы игроки сразу открывались?Для их открытия требуется 2 щелчка мышью.Как бы я это исправить?Видео должны открываться сразу же, а как, и как их получить, нет.
Этот код должен работать, но по какой-то причине это не так.Я не могу понять, что не так, и как мне заставить его работать правильно.
Видео должно появляться на 1 клик, а не на 2. Я не могу понять, как это исправить.
Код:
https://jsfiddle.net/37q96vuj/
const videoPlayer = (function makeVideoPlayer() {
"use strict";
const players = [];
function loadPlayer() {
const tag = document.createElement("script");
tag.src = "https://www.youtube.com/player_api";
const firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
function onPlayerReady(event) {
const player = event.target;
player.setVolume(50); // percent
}
function onPlayerStateChange(event) {
const player = event.target;
if (event.data === YT.PlayerState.PLAYING) {
const otherVideos = (video) => video !== player;
const pauseVideo = (video) => video.pauseVideo();
players.filter(otherVideos).forEach(pauseVideo);
}
const playerVars = player.b.b.playerVars;
if (playerVars.loop && event.data === YT.PlayerState.ENDED) {
player.seekTo(playerVars.start);
}
}
function addVideo(video, desiredPlayerVars) {
const videoId = video.getAttribute("data-id");
const defaultPlayerVars = {
autoplay: 1,
controls: 1,
showinfo: 1,
rel: 0,
iv_load_policy: 3,
cc_load_policy: 0,
fs: 0,
disablekb: 1
};
const playerVars = Object.assign(defaultPlayerVars, desiredPlayerVars);
players.push(new YT.Player(video, {
width: 200,
height: 200,
videoId: videoId,
playerVars,
events: {
"onReady": onPlayerReady,
"onStateChange": onPlayerStateChange
}
}));
}
function init(opts) {
loadPlayer();
addVideo(opts.video);
}
return {
init
};
}());
(function iife() {
"use strict";
const show = (el) => el.classList.remove("hide");
function coverClickHandler(evt) {
const wrapper = evt.currentTarget.nextElementSibling;
show(wrapper);
videoPlayer.init({
video: wrapper.querySelector(".video"),
playerVars: {
start: 900,
end: 1200
}
});
}
const cover = document.querySelector(".playa");
cover.addEventListener("click", coverClickHandler);
}());
(function iife() {
"use strict";
const show = (el) => el.classList.remove("hide");
function coverClickHandler(evt) {
const wrapper = evt.currentTarget.nextElementSibling;
show(wrapper);
videoPlayer.init({
video: wrapper.querySelector(".video"),
playerVars: {
start: 30,
end: 50
}
});
}
const cover = document.querySelector(".playb");
cover.addEventListener("click", coverClickHandler);
}());
(function iife() {
"use strict";
const show = (el) => el.classList.remove("hide");
function coverClickHandler(evt) {
const wrapper = evt.currentTarget.nextElementSibling;
show(wrapper);
videoPlayer.init({
video: wrapper.querySelector(".video"),
playerVars: {
start: 30,
end: 50
}
});
}
const cover = document.querySelector(".playc");
cover.addEventListener("click", coverClickHandler);
}());
(function iife() {
"use strict";
const show = (el) => el.classList.remove("hide");
function coverClickHandler(evt) {
const wrapper = evt.currentTarget.nextElementSibling;
show(wrapper);
videoPlayer.init({
video: wrapper.querySelector(".video"),
playerVars: {
start: 30,
end: 50
}
});
}
const cover = document.querySelector(".playd");
cover.addEventListener("click", coverClickHandler);
}());
(function iife() {
"use strict";
const show = (el) => el.classList.remove("hide");
function coverClickHandler(evt) {
const wrapper = evt.currentTarget.nextElementSibling;
show(wrapper);
videoPlayer.init({
video: wrapper.querySelector(".video"),
playerVars: {
start: 30,
end: 50
}
});
}
const cover = document.querySelector(".playe");
cover.addEventListener("click", coverClickHandler);
}());
(function iife() {
"use strict";
const show = (el) => el.classList.remove("hide");
function coverClickHandler(evt) {
const wrapper = evt.currentTarget.nextElementSibling;
show(wrapper);
videoPlayer.init({
video: wrapper.querySelector(".video"),
playerVars: {
start: 30,
end: 50
}
});
}
const cover = document.querySelector(".playf");
cover.addEventListener("click", coverClickHandler);
}());
(function iife() {
"use strict";
const show = (el) => el.classList.remove("hide");
function coverClickHandler(evt) {
const wrapper = evt.currentTarget.nextElementSibling;
show(wrapper);
videoPlayer.init({
video: wrapper.querySelector(".video"),
playerVars: {
start: 30,
end: 50
}
});
}
const cover = document.querySelector(".playg");
cover.addEventListener("click", coverClickHandler);
}());
(function iife() {
"use strict";
const show = (el) => el.classList.remove("hide");
function coverClickHandler(evt) {
const wrapper = evt.currentTarget.nextElementSibling;
show(wrapper);
videoPlayer.init({
video: wrapper.querySelector(".video"),
playerVars: {
start: 30,
end: 50
}
});
}
const cover = document.querySelector(".playh");
cover.addEventListener("click", coverClickHandler);
}());
(function iife() {
"use strict";
const show = (el) => el.classList.remove("hide");
function coverClickHandler(evt) {
const wrapper = evt.currentTarget.nextElementSibling;
show(wrapper);
videoPlayer.init({
video: wrapper.querySelector(".video"),
playerVars: {
start: 30,
end: 50
}
});
}
const cover = document.querySelector(".playi");
cover.addEventListener("click", coverClickHandler);
}());