Skip to content

Commit 0164d49

Browse files
author
Dario Luna
committed
Fix carousel to wait for video duration before advancing to next slide
1 parent 6df7401 commit 0164d49

File tree

1 file changed

+59
-23
lines changed

1 file changed

+59
-23
lines changed

index.html

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ <h1>Peru JUG</h1>
136136
<script>
137137
// Configuration: Add your media files here
138138
const mediaFiles = [
139-
{ type: 'video', src: 'assets/video1.mp4' },
139+
{ type: 'video', src: 'assets/video.mp4' },
140140
{ type: 'image', src: 'assets/photo1.jpg' },
141141
{ type: 'image', src: 'assets/photo2.jpg' },
142142
{ type: 'image', src: 'assets/photo3.jpg' }
@@ -145,7 +145,7 @@ <h1>Peru JUG</h1>
145145
let currentIndex = 0;
146146
let isPlaying = true;
147147
let rotationInterval;
148-
const rotationDelay = 5000; // 5 seconds
148+
const rotationDelay = 5000; // 5 seconds for images
149149

150150
const mediaWrapper = document.getElementById('mediaWrapper');
151151
const playPauseBtn = document.getElementById('playPauseBtn');
@@ -166,8 +166,15 @@ <h1>Peru JUG</h1>
166166
const video = document.createElement('video');
167167
video.src = media.src;
168168
video.muted = true;
169-
video.loop = true;
169+
video.loop = false; // Don't loop - wait for video to end
170170
video.playsInline = true;
171+
video.preload = 'metadata';
172+
// Listen for video end to advance to next slide
173+
video.addEventListener('ended', () => {
174+
if (isPlaying) {
175+
nextMedia();
176+
}
177+
});
171178
mediaDiv.appendChild(video);
172179
} else {
173180
const img = document.createElement('img');
@@ -180,20 +187,38 @@ <h1>Peru JUG</h1>
180187
});
181188

182189
updateCounter();
183-
startRotation();
190+
updateProgress();
191+
192+
// Start rotation only if first item is not a video
193+
const firstItem = mediaWrapper.querySelector('.media-item.active');
194+
const firstVideo = firstItem?.querySelector('video');
195+
if (!firstVideo && isPlaying) {
196+
startRotation();
197+
} else if (firstVideo && isPlaying) {
198+
// Start playing the video
199+
firstVideo.play().catch(e => console.log('Video play failed:', e));
200+
}
184201
}
185202

186203
// Show specific media item
187204
function showMedia(index) {
188205
const items = mediaWrapper.querySelectorAll('.media-item');
189206

207+
// Stop current rotation timer
208+
stopRotation();
209+
190210
items.forEach((item, i) => {
191211
const video = item.querySelector('video');
192212

193213
if (i === index) {
194214
item.classList.add('active');
195215
if (video && isPlaying) {
216+
video.currentTime = 0; // Reset video to start
196217
video.play().catch(e => console.log('Video play failed:', e));
218+
// Don't start interval for videos - they will advance on 'ended' event
219+
} else if (!video && isPlaying) {
220+
// For images, start the rotation timer
221+
startRotation();
197222
}
198223
} else {
199224
item.classList.remove('active');
@@ -232,10 +257,24 @@ <h1>Peru JUG</h1>
232257
showMedia(newIndex);
233258
}
234259

235-
// Start automatic rotation
260+
// Start automatic rotation (only for images, videos advance on 'ended' event)
236261
function startRotation() {
237262
if (rotationInterval) clearInterval(rotationInterval);
238-
rotationInterval = setInterval(nextMedia, rotationDelay);
263+
264+
// Only start rotation if current media is not a video
265+
const currentItem = mediaWrapper.querySelector('.media-item.active');
266+
const video = currentItem?.querySelector('video');
267+
268+
if (!video && isPlaying) {
269+
rotationInterval = setInterval(() => {
270+
const currentItem = mediaWrapper.querySelector('.media-item.active');
271+
const video = currentItem?.querySelector('video');
272+
// Only advance if it's not a video (videos advance on 'ended' event)
273+
if (!video) {
274+
nextMedia();
275+
}
276+
}, rotationDelay);
277+
}
239278
}
240279

241280
// Stop rotation
@@ -250,42 +289,39 @@ <h1>Peru JUG</h1>
250289
function togglePlayPause() {
251290
isPlaying = !isPlaying;
252291

292+
const currentItem = mediaWrapper.querySelector('.media-item.active');
293+
const video = currentItem?.querySelector('video');
294+
253295
if (isPlaying) {
254296
playPauseIcon.textContent = '⏸';
255297
playPauseBtn.innerHTML = '<span id="playPauseIcon">⏸</span> Pause';
256-
startRotation();
257298

258-
// Resume current video if any
259-
const currentItem = mediaWrapper.querySelector('.media-item.active');
260-
const video = currentItem?.querySelector('video');
261-
if (video) video.play().catch(e => console.log('Video play failed:', e));
299+
if (video) {
300+
// Resume video
301+
video.play().catch(e => console.log('Video play failed:', e));
302+
} else {
303+
// Start rotation for images
304+
startRotation();
305+
}
262306
} else {
263307
playPauseIcon.textContent = '▶';
264308
playPauseBtn.innerHTML = '<span id="playPauseIcon">▶</span> Play';
265309
stopRotation();
266310

267311
// Pause current video if any
268-
const currentItem = mediaWrapper.querySelector('.media-item.active');
269-
const video = currentItem?.querySelector('video');
270312
if (video) video.pause();
271313
}
272314
}
273315

274316
// Event listeners
275317
playPauseBtn.addEventListener('click', togglePlayPause);
276318
nextBtn.addEventListener('click', () => {
277-
nextMedia();
278-
if (isPlaying) {
279-
stopRotation();
280-
startRotation();
281-
}
319+
stopRotation(); // Stop current timer
320+
nextMedia(); // This will start appropriate rotation in showMedia()
282321
});
283322
prevBtn.addEventListener('click', () => {
284-
prevMedia();
285-
if (isPlaying) {
286-
stopRotation();
287-
startRotation();
288-
}
323+
stopRotation(); // Stop current timer
324+
prevMedia(); // This will start appropriate rotation in showMedia()
289325
});
290326

291327
// Initialize

0 commit comments

Comments
 (0)