75 lines
1.9 KiB
JavaScript
75 lines
1.9 KiB
JavaScript
define([], function () {
|
|
'use strict';
|
|
|
|
return function (config, element) {
|
|
var video = element.querySelector('video');
|
|
var isPlaying = false;
|
|
|
|
if (!video || element.getAttribute('data-video-hover-bound') === 'true') {
|
|
return;
|
|
}
|
|
|
|
element.setAttribute('data-video-hover-bound', 'true');
|
|
video.muted = true;
|
|
video.playsInline = true;
|
|
video.setAttribute('muted', 'muted');
|
|
video.setAttribute('playsinline', 'playsinline');
|
|
|
|
function playVideo() {
|
|
if (isPlaying) {
|
|
return;
|
|
}
|
|
|
|
isPlaying = true;
|
|
|
|
try {
|
|
video.currentTime = 0;
|
|
} catch (error) {}
|
|
|
|
element.classList.add('is-playing');
|
|
video.play().catch(function () {
|
|
isPlaying = false;
|
|
element.classList.remove('is-playing');
|
|
});
|
|
}
|
|
|
|
function stopVideo() {
|
|
isPlaying = false;
|
|
video.pause();
|
|
|
|
try {
|
|
video.currentTime = 0;
|
|
} catch (error) {}
|
|
|
|
element.classList.remove('is-playing');
|
|
}
|
|
|
|
element.addEventListener('mouseover', function (event) {
|
|
if (element.contains(event.relatedTarget)) {
|
|
return;
|
|
}
|
|
|
|
playVideo();
|
|
});
|
|
|
|
element.addEventListener('mouseout', function (event) {
|
|
if (element.contains(event.relatedTarget)) {
|
|
return;
|
|
}
|
|
|
|
stopVideo();
|
|
});
|
|
|
|
element.addEventListener('pointerenter', playVideo);
|
|
element.addEventListener('pointerleave', stopVideo);
|
|
|
|
element.addEventListener('touchstart', function (event) {
|
|
if (event.target.closest('a')) {
|
|
return;
|
|
}
|
|
|
|
playVideo();
|
|
}, {passive: true});
|
|
};
|
|
});
|