<div class="autoplay-hero-video " data-behavior="autoplay-hero-video" data-video-type="youtube" data-video-id="ufLSGCZEPrg">
<div class="autoplay-hero-video__player-wrap">
<div id="vimeo-player" class="vimeo-video-player"></div>
<img src="/assets/example-content/autoplay-hero-error.png" />
</div>
<div class="control-overlay">
<div class="play-pause-controls">
<a class="play-pause-controls__play hidden" role="video play" aria-label="video play" tabindex="0" aria-selected="true"></a>
<a class="play-pause-controls__pause" role="video pause button" aria-label="video pause button play" tabindex="0" aria-selected="true"></a>
</div>
<div class="mute-unmute-controls">
<a class="mute-unmute-controls__mute hidden" role="mute" aria-label="mute" aria-selected="true" tabindex="0"></a>
<a class="mute-unmute-controls__unmute" role="unmute button" aria-label="it isunmute pleasemute" tabindex="0" aria-selected="true"></a>
</div>
</div>
<div class="autoplay-hero-video__gradient"></div>
<h3 class="autoplay-hero-video__textoverlay">ENGLAND FOOTBALL IS COMING HOME</h3>
</div>
No notes defined.
{
"shallow": "",
"videoId": "ufLSGCZEPrg",
"type": "youtube"
}
import Player from '@vimeo/player';
import { loadYouTubeApi } from '../video/video';
export default parentElement => {
const { videoId, videoType } = parentElement.dataset;
const videoPlayerId = parentElement
.querySelector('.vimeo-video-player')
.getAttribute('id');
const playButton = parentElement.querySelector('.play-pause-controls__play');
const muteButton = parentElement.querySelector('.mute-unmute-controls__mute');
const pauseButton = parentElement.querySelector(
'.play-pause-controls__pause'
);
const unmuteButton = parentElement.querySelector(
'.mute-unmute-controls__unmute'
);
// eslint-disable-next-line no-unused-vars
let videoPlayer;
if (!videoId) {
return;
}
const resizeVideo = () => {
const headerHeight = document.querySelector('.global-fixed-header')
.offsetHeight;
const screenWidth = document.getElementsByTagName('body')[0].clientWidth;
const screenHeight = window.innerHeight - headerHeight;
const playerWidth = screenHeight * (16 / 9);
if (playerWidth < screenWidth && parentElement.querySelector('iframe')) {
const playerHeight = screenWidth * (9 / 16);
const PlayerWidth = screenWidth;
// eslint-disable-next-line no-param-reassign
parentElement.querySelector('iframe').style.height = `${playerHeight}px`;
// eslint-disable-next-line no-param-reassign
parentElement.querySelector('iframe').style.width = `${PlayerWidth}px`;
}
};
const onPlayerError = () => {
parentElement.classList.remove('vimeo-player-loaded');
};
const onPlayerStateChange = event => {
if (event.data === window.YT.PlayerState.ENDED) {
videoPlayer.playVideo();
}
};
const onPlayerReady = () => {
// To capture error on video
videoPlayer.playVideo();
videoPlayer.mute();
parentElement.classList.add('vimeo-player-loaded');
resizeVideo();
};
const initVideo = () => {
videoPlayer = new window.YT.Player(videoPlayerId, {
height: '390',
width: '640',
videoId,
playerVars: {
autoplay: 1,
playsinline: 1,
controls: 0,
loop: 1,
},
events: {
onReady: onPlayerReady,
onError: onPlayerError,
onStateChange: onPlayerStateChange,
},
});
};
if (videoType === 'youtube') {
if (
window.YouTubeIframeApiReady === undefined ||
window.YouTubeIframeApiReady === false
) {
loadYouTubeApi();
window.onYouTubeIframeAPIReady = () => {
window.YouTubeIframeApiReady = true;
initVideo();
};
} else {
initVideo();
}
} else {
// eslint-disable-next-line no-undef
videoPlayer = new Player(videoPlayerId, {
id: videoId,
autoplay: 1,
muted: 1,
title: 0,
sidedock: 0,
controls: 0,
loop: 1,
quality: 'auto',
width: '100%',
height: '100%',
});
videoPlayer
.play()
.then(() => {
parentElement.classList.add('vimeo-player-loaded');
// The video is loadded
resizeVideo();
})
.catch(() => {
// console.log(error.name);
});
}
playButton.addEventListener('click', () => {
if (videoType === 'youtube') {
videoPlayer.playVideo();
} else {
videoPlayer.play();
}
playButton.classList.toggle('hidden');
pauseButton.classList.toggle('hidden');
});
playButton.addEventListener('keyup', event => {
if (event.keyCode === 13) {
// Cancel the default action, if needed
event.preventDefault();
playButton.click();
}
});
pauseButton.addEventListener('click', () => {
if (videoType === 'youtube') {
videoPlayer.pauseVideo();
} else {
videoPlayer.pause();
}
playButton.classList.toggle('hidden');
pauseButton.classList.toggle('hidden');
});
pauseButton.addEventListener('keyup', event => {
if (event.keyCode === 13) {
// Cancel the default action, if needed
event.preventDefault();
pauseButton.click();
}
});
muteButton.addEventListener('click', () => {
if (videoType === 'youtube') {
videoPlayer.mute();
} else {
videoPlayer.setMuted(1);
}
muteButton.classList.toggle('hidden');
unmuteButton.classList.toggle('hidden');
});
muteButton.addEventListener('keyup', event => {
if (event.keyCode === 13) {
// Cancel the default action, if needed
event.preventDefault();
muteButton.click();
}
});
unmuteButton.addEventListener('click', () => {
if (videoType === 'youtube') {
videoPlayer.unMute();
} else {
videoPlayer.setMuted(0);
}
muteButton.classList.toggle('hidden');
unmuteButton.classList.toggle('hidden');
});
unmuteButton.addEventListener('keyup', event => {
if (event.keyCode === 13) {
// Cancel the default action, if needed
event.preventDefault();
unmuteButton.click();
}
});
if (window.innerWidth < 820) {
parentElement
.querySelector('.autoplay-hero-video__gradient')
.addEventListener('click', () => {
if (
window
.getComputedStyle(parentElement.querySelector('.control-overlay'))
.getPropertyValue('opacity') === '1'
) {
// eslint-disable-next-line no-param-reassign
parentElement.querySelector('.control-overlay').style.opacity = '0';
// eslint-disable-next-line no-param-reassign
parentElement.querySelector(
'.autoplay-hero-video__gradient'
).style.opacity = '0';
} else {
// eslint-disable-next-line no-param-reassign
parentElement.querySelector('.control-overlay').style.opacity = '1';
// eslint-disable-next-line no-param-reassign
parentElement.querySelector(
'.autoplay-hero-video__gradient'
).style.opacity = '1';
}
});
}
window.addEventListener('resize', () => resizeVideo());
};
.autoplay-hero-video {
position: relative;
width: 100%;
height: calc(100vh - 64px);
z-index: 0;
&__player-wrap {
z-index: 10;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
overflow: hidden;
iframe {
object-fit: cover;
width: 177.77%;
height: 100vw !important;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
pointer-events: none;
.player {
width: 100% !important;
height: 100% !important;
object-fit: cover !important;
}
}
img {
object-fit: cover;
width: 100%;
height: 100%;
}
.vimeo-video-player {
display: none;
}
}
@mixin transition($params) {
-webkit-transition: $params;
-moz-transition: $params;
-khtml-transition: $params;
-o-transition: $params;
transition: $params;
}
.control-overlay {
position: absolute;
bottom: 3.5rem;
z-index: 14;
width: 100%;
opacity: 0;
transition: opacity 0.3s ease-in-out;
min-height: 4rem;
display: none;
a {
display: inline-block;
}
a.hidden {
display: none;
}
.play-pause-controls {
float: left;
margin-left: 4.2rem;
&__play {
background: url(./assets/images/video-play-button.svg) no-repeat center;
width: 2.5rem;
height: 2.9rem;
background-size: 100%;
@include transition(transform 0.3s ease-in-out);
&:hover {
transform: scale(1.2);
}
&:active {
transform: scale(1.2);
}
}
&__pause {
background: url(./assets/images/video-pause-button.svg) no-repeat center;
width: 1.9rem;
height: 3.4rem;
background-size: 100%;
@include transition(transform 0.3s ease-in-out);
&:hover {
transform: scale(1.2);
}
&:active {
transform: scale(1.2);
}
}
}
.mute-unmute-controls {
float: right;
margin-right: 3.2rem;
&__mute {
background: url(./assets/images/video-sound-on.svg) no-repeat 3px center;
width: 4.4rem;
height: 3.2rem;
background-size: 100%;
@include transition(transform 0.3s ease-in-out);
&:hover {
transform: scale(1.2);
}
&:active {
transform: scale(1.2);
}
}
&__unmute {
background: url(./assets/images/video-sound-off.svg) no-repeat center;
width: 4.2rem;
height: 3.1rem;
background-size: 100%;
@include transition(transform 0.3s ease-in-out);
&:hover {
transform: scale(1.2);
}
&:active {
transform: scale(1.2);
}
}
}
}
&__textoverlay {
@extend .heading-1;
color: $white;
position: absolute;
z-index: 11;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
padding: 0 2.4rem;
}
&__gradient {
bottom: 0;
content: '';
height: 100%;
top: 0;
width: 100%;
position: absolute;
z-index: 13;
opacity: 0;
transition: opacity 0.3s ease-in-out;
display: none;
&::after {
background: linear-gradient(
180deg,
rgba(0, 0, 0, 0) 45.31%,
rgba(1, 30, 65, 0.204) 68.68%,
rgba(1, 30, 65, 0.6) 94.79%
);
bottom: 0;
content: '';
display: block;
height: 100%;
position: absolute;
top: 0;
width: 100%;
}
}
&.vimeo-player-loaded {
.autoplay-hero-video__player-wrap {
.vimeo-video-player {
display: block;
}
img {
display: none;
}
}
.control-overlay {
display: block;
}
.autoplay-hero-video__gradient {
display: block;
}
}
&.shallow {
height: 42.4rem !important;
background-color: rgba(1, 30, 65, 0.3);
}
@media screen and (max-width: $mq-medium) and (orientation: portrait) {
iframe {
height: 100vh !important;
width: 177.77vh;
}
}
@media screen and (min-width: $mq-medium) and (orientation: portrait) {
iframe {
height: 100vh;
width: 177.77vh;
}
}
@media screen and (min-width: $mq-medium) {
height: calc(100vh - 81px);
&__textoverlay {
font-size: 7.8rem;
line-height: 6.8rem;
padding: 0;
}
&.vimeo-player-loaded:hover {
.control-overlay {
opacity: 1;
}
.autoplay-hero-video__gradient {
opacity: 1;
}
}
/* stylelint-disable no-descending-specificity */
&.shallow {
height: 48rem !important;
iframe {
width: 1400px !important;
height: calc(1400px * 0.5625) !important;
}
.control-overlay {
width: 960px;
margin: 0 auto;
left: 0;
right: 0;
}
}
}
@media screen and (min-width: 820px) and (max-width: 980px) {
.control-overlay {
width: 100% !important;
}
}
@media screen and (min-width: 1100px) {
&.shallow {
.autoplay-hero-video__textoverlay {
left: 0;
right: 0;
margin: 0 auto;
}
}
}
}
.fixed-banner {
.autoplay-hero-video {
position: fixed;
top: 64px;
height: 57.6rem;
@media screen and (min-width: $mq-medium) {
top: 81px;
height: 54.6rem;
}
@media screen and (min-width: 820px) and (max-width: 980px) {
height: 46.3rem;
}
@media screen and (min-width: 1440px) {
height: 62rem;
}
}
}
.shallow-banner {
max-height: 40.5rem !important;
@media screen and (min-width: $mq-medium) {
max-height: 47.1rem !important;
}
}
<div class="autoplay-hero-video {{#if shallow}}shallow{{/if}}" data-behavior="autoplay-hero-video" data-video-type="{{type}}" data-video-id="{{videoId}}">
<div class="autoplay-hero-video__player-wrap">
<div id="vimeo-player" class="vimeo-video-player"></div>
<img src="/assets/example-content/autoplay-hero-error.png" />
</div>
<div class="control-overlay">
<div class="play-pause-controls">
<a class="play-pause-controls__play hidden" role="video play" aria-label="video play" tabindex="0" aria-selected="true"></a>
<a class="play-pause-controls__pause" role="video pause button" aria-label="video pause button play" tabindex="0" aria-selected="true"></a>
</div>
<div class="mute-unmute-controls">
<a class="mute-unmute-controls__mute hidden" role="mute" aria-label="mute" aria-selected="true" tabindex="0"></a>
<a class="mute-unmute-controls__unmute" role="unmute button" aria-label="it isunmute pleasemute" tabindex="0" aria-selected="true"></a>
</div>
</div>
<div class="autoplay-hero-video__gradient"></div>
<h3 class="autoplay-hero-video__textoverlay">ENGLAND FOOTBALL IS COMING HOME</h3>
</div>