Performance: Carousel indicators
Carousel indicators affect the interaction responsiveness and visual stability during slide changes in a carousel component.
<script>const indicators = document.querySelector('.carousel-indicators'); indicators.addEventListener('click', e => { if(e.target.tagName === 'BUTTON'){ indicators.querySelector('.active')?.classList.remove('active'); e.target.classList.add('active'); } });</script>
<script>document.querySelectorAll('.carousel-indicators button').forEach(btn => btn.addEventListener('click', () => { document.querySelectorAll('.carousel-indicators button').forEach(b => b.classList.remove('active')); btn.classList.add('active'); }));</script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple event listeners on each indicator | Many event listeners, multiple class changes | N reflows per click | High paint cost due to frequent style changes | [X] Bad |
| Single event listener with event delegation | One event listener, two class changes per click | 1 reflow per click | Low paint cost, minimal style recalculations | [OK] Good |
carousel indicators in Bootstrap?<button> elements for carousel indicators to ensure proper ARIA roles and keyboard focus.<div class="carousel-indicators"> <button type="button" data-bs-target="#myCarousel" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button> <button type="button" data-bs-target="#myCarousel" data-bs-slide-to="1" aria-label="Slide 2"></button> <button type="button" data-bs-target="#myCarousel" data-bs-slide-to="2" aria-label="Slide 3"></button> </div>
data-bs-slide-to="1" means the button targets the slide with index 1 (second slide, zero-based).<div class="carousel-indicators"> <button type="button" data-bs-slide-to="0" aria-label="Slide 1"></button> <button type="button" data-bs-slide-to="1" aria-label="Slide 2"></button> <button type="button" data-bs-slide-to="2" aria-label="Slide 3"></button> </div>
data-bs-target pointing to the carousel's ID to link the button to the carousel.data-bs-target, Bootstrap cannot know which carousel to control, so indicators won't work.data-bs-target attribute on buttons -> Option Bclass="active" and aria-current="true" for accessibility.Pass the test and this pattern moves unknown