feat: a video on several products, sound and expand on the floating player, upright vertical video, looping cards at the foot of the home (Rodada 5, D7)
site_videos.products (the old product column is kept and copied); one floating video per product checks the overlap and names it. The floating player starts muted, with a sound button that says its state and an expand button to the large player. A vertical video stands upright on the floating and the large player. The home's videos move to its foot as muted looping cards that play only on screen, not at all with reduced motion, and open the large player. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -20,6 +20,16 @@ const VIDEOS = [
|
||||
src: null, captions: null},
|
||||
];
|
||||
|
||||
// A video may appear on more than one product page; the old single `product`
|
||||
// still stands for one product.
|
||||
function videoProducts(video) {
|
||||
return video.products && video.products.length ? video.products : [video.product].filter(Boolean);
|
||||
}
|
||||
|
||||
const SOUND_OFF = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 9h3l4-4v14l-4-4H4z"/><path d="M15 9l6 6M21 9l-6 6"/></svg>';
|
||||
const SOUND_ON = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 9h3l4-4v14l-4-4H4z"/><path d="M15 8a5.5 5.5 0 0 1 0 8"/><path d="M18 5a9 9 0 0 1 0 14"/></svg>';
|
||||
const EXPAND = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5"/></svg>';
|
||||
|
||||
function videoElement(video, {controls = false} = {}) {
|
||||
const el = document.createElement('video');
|
||||
el.src = video.src;
|
||||
@@ -40,7 +50,29 @@ function videoElement(video, {controls = false} = {}) {
|
||||
return el;
|
||||
}
|
||||
|
||||
// The category shows only videos with a published file.
|
||||
// The home cards: every published video plays muted, in loop, and opens the
|
||||
// large player on click. Only the cards on screen play.
|
||||
const reduceMotion = matchMedia('(prefers-reduced-motion: reduce)');
|
||||
const cardPlayers = new WeakMap();
|
||||
let cardObserver = null;
|
||||
function observeCards() {
|
||||
const grid = document.getElementById('videosGrid');
|
||||
if (!grid) return;
|
||||
if (!cardObserver) {
|
||||
cardObserver = new IntersectionObserver(entries => {
|
||||
for (const entry of entries) {
|
||||
const player = cardPlayers.get(entry.target);
|
||||
if (!player) continue;
|
||||
if (entry.isIntersecting && !reduceMotion.matches) player.play().catch(() => {});
|
||||
else player.pause();
|
||||
}
|
||||
}, {threshold: 0.5});
|
||||
}
|
||||
cardObserver.disconnect();
|
||||
for (const card of grid.querySelectorAll('.videoCard')) cardObserver.observe(card);
|
||||
}
|
||||
|
||||
// The section shows only videos with a published file.
|
||||
function videosSection() {
|
||||
const grid = document.getElementById('videosGrid');
|
||||
if (!grid) return;
|
||||
@@ -52,19 +84,23 @@ function videosSection() {
|
||||
card.type = 'button';
|
||||
card.className = 'videoCard';
|
||||
card.dataset.video = video.id;
|
||||
const play = document.createElement('span');
|
||||
play.className = 'vcPlay';
|
||||
play.textContent = '▶';
|
||||
const player = videoElement(video);
|
||||
player.loop = true;
|
||||
player.addEventListener('loadedmetadata', () => {
|
||||
if (player.videoHeight > player.videoWidth) card.classList.add('vertical');
|
||||
});
|
||||
// The loop stops at 30 s, like the rest of the catalog.
|
||||
player.addEventListener('timeupdate', () => { if (player.currentTime >= 30) player.currentTime = 0; });
|
||||
const title = document.createElement('b');
|
||||
title.textContent = video.title;
|
||||
const desc = document.createElement('span');
|
||||
desc.textContent = video.desc;
|
||||
const note = document.createElement('em');
|
||||
note.textContent = 'Assistir · 30 s';
|
||||
card.append(play, title, desc, note);
|
||||
card.append(player, title, desc);
|
||||
card.addEventListener('click', () => openVideo(video, card));
|
||||
cardPlayers.set(card, player);
|
||||
grid.append(card);
|
||||
}
|
||||
observeCards();
|
||||
}
|
||||
videosSection();
|
||||
|
||||
@@ -96,7 +132,11 @@ function openVideo(video, trigger=document.activeElement) {
|
||||
}
|
||||
const box = videoModal.querySelector('.vmBox');
|
||||
box.querySelectorAll('video').forEach(el => el.remove());
|
||||
videoModal.classList.remove('vertical');
|
||||
const player = videoElement(video, {controls: true});
|
||||
player.addEventListener('loadedmetadata', () => {
|
||||
if (player.videoHeight > player.videoWidth) videoModal.classList.add('vertical');
|
||||
});
|
||||
player.addEventListener('timeupdate', () => { if (player.currentTime >= 30) player.pause(); });
|
||||
box.append(player);
|
||||
videoModal.hidden = false;
|
||||
@@ -119,7 +159,7 @@ function closeVideo() {
|
||||
let floating = null, floatingId = null;
|
||||
function videoForProduct() {
|
||||
if (document.documentElement.dataset.rota !== 'produto') return null;
|
||||
return VIDEOS.find(video => video.src && video.product === modo) || null;
|
||||
return VIDEOS.find(video => video.src && videoProducts(video).includes(modo)) || null;
|
||||
}
|
||||
function syncFloatingVideo() {
|
||||
const video = videoForProduct();
|
||||
@@ -132,6 +172,16 @@ function syncFloatingVideo() {
|
||||
floatingId = video.id;
|
||||
floating = document.createElement('div');
|
||||
floating.className = 'videoFloat';
|
||||
const actions = document.createElement('div');
|
||||
actions.className = 'vfActions';
|
||||
const sound = document.createElement('button');
|
||||
sound.type = 'button';
|
||||
sound.className = 'vfSound';
|
||||
const expand = document.createElement('button');
|
||||
expand.type = 'button';
|
||||
expand.className = 'vfExpand';
|
||||
expand.setAttribute('aria-label', 'Expandir vídeo');
|
||||
expand.innerHTML = EXPAND;
|
||||
const close = document.createElement('button');
|
||||
close.type = 'button';
|
||||
close.className = 'vfClose';
|
||||
@@ -143,6 +193,28 @@ function syncFloatingVideo() {
|
||||
player.loop = false;
|
||||
// 30 segundos no máximo, como o PDF pede.
|
||||
player.addEventListener('timeupdate', () => { if (player.currentTime >= 30) player.pause(); });
|
||||
player.addEventListener('loadedmetadata', () => {
|
||||
if (player.videoHeight > player.videoWidth) floating.classList.add('vertical');
|
||||
});
|
||||
// It starts muted; the button turns the sound on and off.
|
||||
let muted = true;
|
||||
const paintSound = () => {
|
||||
sound.setAttribute('aria-pressed', String(!muted));
|
||||
sound.setAttribute('aria-label', muted ? 'Ativar som' : 'Desativar som');
|
||||
sound.innerHTML = muted ? SOUND_OFF : SOUND_ON;
|
||||
};
|
||||
paintSound();
|
||||
sound.addEventListener('click', () => {
|
||||
muted = !muted;
|
||||
player.muted = muted;
|
||||
paintSound();
|
||||
desc.textContent = (muted ? 'Mudo' : 'Com som') + (video.captions ? ' · com legenda' : '') + ' · 30 s';
|
||||
});
|
||||
expand.addEventListener('click', () => {
|
||||
player.pause();
|
||||
openVideo(video, expand);
|
||||
});
|
||||
actions.append(sound, expand, close);
|
||||
const label = document.createElement('div');
|
||||
label.className = 'vfLabel';
|
||||
const title = document.createElement('b');
|
||||
@@ -150,7 +222,7 @@ function syncFloatingVideo() {
|
||||
const desc = document.createElement('span');
|
||||
desc.textContent = video.captions ? 'Mudo · com legenda · 30 s' : 'Mudo · 30 s';
|
||||
label.append(title, desc);
|
||||
floating.append(player, close, label);
|
||||
floating.append(player, actions, label);
|
||||
document.body.append(floating);
|
||||
player.play().catch(() => {});
|
||||
}
|
||||
@@ -160,7 +232,8 @@ syncFloatingVideo();
|
||||
window.siteVideosReady=fetch('/api/site/videos').then(response=>response.ok?response.json():null).then(data=>{
|
||||
for(const published of data?.videos||[]){
|
||||
const video=VIDEOS.find(item=>item.id===published.id);
|
||||
if(video){video.src=published.src;video.captions=published.captions;video.product=published.product;}
|
||||
if(video){video.src=published.src;video.captions=published.captions;
|
||||
video.product=published.product;video.products=published.products||[];}
|
||||
}
|
||||
videosSection();
|
||||
syncFloatingVideo();
|
||||
|
||||
Reference in New Issue
Block a user