<script>
(function () {
// ======== ССЫЛКА НА ВИДЕО — МЕНЯТЬ ЗДЕСЬ ========
var videoURL = 'https://static.tildacdn.com/vide3861-3765-4334-a265-306337386161/b____1.mp4';
// ======== НАСТРОЙКИ ЗАТЕМНЕНИЯ ========
var overlayColor = '#000000'; // цвет затемнения (в HEX, например #000000 — чёрный)
var overlayOpacity = 0.8; // прозрачность: 0 — нет затемнения, 1 — сплошной цвет
// ======================================
function hexToRgba(hex, alpha) {
hex = hex.replace('#', '');
if (hex.length === 3) {
hex = hex.split('').map(function (c) { return c + c; }).join('');
}
var r = parseInt(hex.substring(0, 2), 16);
var g = parseInt(hex.substring(2, 4), 16);
var b = parseInt(hex.substring(4, 6), 16);
return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + alpha + ')';
}
function mountVideo(container) {
if (container.dataset.videoMounted) return; // защита от повтора
container.dataset.videoMounted = 'true';
var box = container.closest('.tn-elem') || container;
if (getComputedStyle(box).position === 'static') {
box.style.position = 'relative';
}
box.style.overflow = 'hidden';
var video = document.createElement('video');
video.src = videoURL;
video.loop = true;
video.autoplay = true;
video.muted = true; // без muted браузеры блокируют автозапуск
video.defaultMuted = true;
video.preload = 'auto';
video.setAttribute('muted', '');
video.setAttribute('playsinline', '');
video.setAttribute('webkit-playsinline', '');
video.style.cssText = 'position:absolute;top:0;left:0;width:100%;height:100%;' +
'object-fit:cover;object-position:center;display:block;';
box.appendChild(video);
// Слой затемнения поверх видео
var overlay = document.createElement('div');
var bgColor = overlayColor.charAt(0) === '#'
? hexToRgba(overlayColor, overlayOpacity) // HEX + прозрачность
: overlayColor; // например 'rgba(0,0,0,0.4)' — берётся как есть
overlay.style.cssText = 'position:absolute;top:0;left:0;width:100%;height:100%;' +
'background:' + bgColor + ';pointer-events:none;z-index:10;';
box.appendChild(overlay);
// Если автозапуск заблокирован — стартуем по первому клику/тапу
var p = video.play();
if (p && p.catch) {
p.catch(function () {
var unlock = function () { video.play(); };
document.addEventListener('click', unlock, { once: true });
document.addEventListener('touchstart', unlock, { once: true });
});
}
video.addEventListener('error', function () {
console.error('[custo-video] Не удалось загрузить видео: ' + videoURL);
});
}
var tries = 0;
var timer = setInterval(function () {
var all = document.querySelectorAll('.custo-video');
var mounted = document.querySelectorAll('.custo-video[data-video-mounted]');
for (var i = 0; i < all.length; i++) mountVideo(all[i]);
if (all.length > 0 && all.length === mounted.length) clearInterval(timer);
if (++tries >= 150) {
clearInterval(timer);
if (all.length === 0) {
console.warn('[custo-video] Элемент с классом custo-video не найден. Проверь класс и публикацию страницы.');
}
}
}, 100);
})();
</script>