|
| 1 | +/*! |
| 2 | + * Carousel — vanilla JS, no jQuery / Bootstrap JS. |
| 3 | + * |
| 4 | + * Auto-rotating crossfade banner with clickable indicator dots, replacing |
| 5 | + * Bootstrap 3's carousel plugin (Track A, see issues #1288 / #1253). It works |
| 6 | + * with the existing markup: a `.carousel` containing `.carousel-inner > .item` |
| 7 | + * slides (one marked `.active`) and an optional `.carousel-indicators > li` |
| 8 | + * dot list. The crossfade itself is pure CSS (carousel_fade.css); this script |
| 9 | + * only moves the `.active` class and runs the autoplay timer. |
| 10 | + * |
| 11 | + * Per-carousel options (data attributes on the `.carousel` element): |
| 12 | + * - data-interval: autoplay delay in ms (default 5000; <= 0 disables autoplay). |
| 13 | + * - data-pause="false": do NOT pause on mouse hover (default is to pause). |
| 14 | + * |
| 15 | + * Accessibility: |
| 16 | + * - Honors prefers-reduced-motion: no autoplay and (via CSS) no crossfade. |
| 17 | + * - Pauses while focus is inside the carousel (keyboard users reading links) |
| 18 | + * and while the browser tab is hidden. |
| 19 | + * - Marks the active indicator dot with aria-current. |
| 20 | + * |
| 21 | + * Note: there are no prev/next arrows or swipe gestures — this matches the |
| 22 | + * previous Bootstrap setup, which had neither. |
| 23 | + */ |
| 24 | +(function () { |
| 25 | + 'use strict'; |
| 26 | + |
| 27 | + var prefersReducedMotion = window.matchMedia |
| 28 | + ? window.matchMedia('(prefers-reduced-motion: reduce)').matches |
| 29 | + : false; |
| 30 | + |
| 31 | + function setupCarousel(root) { |
| 32 | + var inner = root.querySelector('.carousel-inner'); |
| 33 | + if (!inner) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + var slides = Array.prototype.slice.call(inner.querySelectorAll(':scope > .item')); |
| 38 | + if (slides.length === 0) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + var indicators = Array.prototype.slice.call( |
| 43 | + root.querySelectorAll('.carousel-indicators > li') |
| 44 | + ); |
| 45 | + |
| 46 | + // Start from whichever slide the template marked active (default first). |
| 47 | + var current = slides.findIndex(function (slide) { |
| 48 | + return slide.classList.contains('active'); |
| 49 | + }); |
| 50 | + if (current < 0) { |
| 51 | + current = 0; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Shows the slide at `index`, updates the indicator dots, and plays only |
| 56 | + * the active slide's video (if any) to avoid decoding hidden videos. |
| 57 | + */ |
| 58 | + function render(index) { |
| 59 | + slides.forEach(function (slide, i) { |
| 60 | + var isActive = i === index; |
| 61 | + slide.classList.toggle('active', isActive); |
| 62 | + |
| 63 | + var video = slide.querySelector('video'); |
| 64 | + if (video) { |
| 65 | + if (isActive) { |
| 66 | + var playback = video.play(); |
| 67 | + if (playback && playback.catch) { |
| 68 | + playback.catch(function () { /* autoplay blocked — ignore */ }); |
| 69 | + } |
| 70 | + } else { |
| 71 | + video.pause(); |
| 72 | + } |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + indicators.forEach(function (dot, i) { |
| 77 | + var isActive = i === index; |
| 78 | + dot.classList.toggle('active', isActive); |
| 79 | + if (isActive) { |
| 80 | + dot.setAttribute('aria-current', 'true'); |
| 81 | + } else { |
| 82 | + dot.removeAttribute('aria-current'); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + current = index; |
| 87 | + } |
| 88 | + |
| 89 | + render(current); |
| 90 | + |
| 91 | + // A single slide has nothing to rotate or navigate. |
| 92 | + if (slides.length < 2) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + /* ----------------------------- Autoplay ----------------------------- */ |
| 97 | + |
| 98 | + var intervalAttr = root.getAttribute('data-interval'); |
| 99 | + var interval = intervalAttr == null ? 5000 : parseInt(intervalAttr, 10); |
| 100 | + var pauseOnHover = root.getAttribute('data-pause') !== 'false'; |
| 101 | + |
| 102 | + var timer = null; |
| 103 | + var paused = false; |
| 104 | + |
| 105 | + function showNext() { |
| 106 | + render((current + 1) % slides.length); |
| 107 | + } |
| 108 | + |
| 109 | + function canPlay() { |
| 110 | + return !prefersReducedMotion && interval > 0 && !paused && !document.hidden; |
| 111 | + } |
| 112 | + |
| 113 | + function start() { |
| 114 | + stop(); |
| 115 | + if (canPlay()) { |
| 116 | + timer = window.setInterval(showNext, interval); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + function stop() { |
| 121 | + if (timer !== null) { |
| 122 | + window.clearInterval(timer); |
| 123 | + timer = null; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + function restart() { |
| 128 | + // After a manual jump, give the next auto-advance a full interval. |
| 129 | + stop(); |
| 130 | + start(); |
| 131 | + } |
| 132 | + |
| 133 | + /* ---------------------------- Indicators ---------------------------- */ |
| 134 | + |
| 135 | + indicators.forEach(function (dot, i) { |
| 136 | + dot.addEventListener('click', function () { |
| 137 | + render(i); |
| 138 | + restart(); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + /* -------------------------- Pause conditions ------------------------ */ |
| 143 | + |
| 144 | + if (pauseOnHover) { |
| 145 | + root.addEventListener('mouseenter', function () { |
| 146 | + paused = true; |
| 147 | + stop(); |
| 148 | + }); |
| 149 | + root.addEventListener('mouseleave', function () { |
| 150 | + paused = false; |
| 151 | + start(); |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + // Pause while focus is inside the carousel (keyboard users on slide links). |
| 156 | + root.addEventListener('focusin', function () { |
| 157 | + paused = true; |
| 158 | + stop(); |
| 159 | + }); |
| 160 | + root.addEventListener('focusout', function (event) { |
| 161 | + if (!root.contains(event.relatedTarget)) { |
| 162 | + paused = false; |
| 163 | + start(); |
| 164 | + } |
| 165 | + }); |
| 166 | + |
| 167 | + // Pause when the tab is hidden; resume when it becomes visible again. |
| 168 | + document.addEventListener('visibilitychange', function () { |
| 169 | + if (document.hidden) { |
| 170 | + stop(); |
| 171 | + } else { |
| 172 | + start(); |
| 173 | + } |
| 174 | + }); |
| 175 | + |
| 176 | + start(); |
| 177 | + } |
| 178 | + |
| 179 | + document.addEventListener('DOMContentLoaded', function () { |
| 180 | + document.querySelectorAll('.carousel').forEach(setupCarousel); |
| 181 | + }); |
| 182 | +})(); |
0 commit comments