Autoplay slider with progressbar pagination and a play/pause toggle for better accessibility and user control.
帶有進度條分頁的自動播放輪播,新增播放/暫停切換按鈕,提升無障礙性與使用者控制體驗。
var DS = DriftSlider;
var slider = new DS.DriftSlider('.drift-slider', {
modules: [DS.Navigation, DS.Pagination, DS.Autoplay, DS.A11y],
loop: true,
speed: 400,
grabCursor: true,
autoplay: {
enabled: true,
delay: 3000,
disableOnInteraction: false,
},
pagination: {
type: 'progressbar',
},
});
// Play/Pause toggle
var playing = true;
var btn = document.getElementById('btn-play-pause');
function updateButton() {
btn.setAttribute('aria-label',
playing ? 'Pause autoplay' : 'Resume autoplay');
btn.classList.toggle('is-paused', !playing);
// Toggle icon and label visibility
iconPause.style.display = playing ? '' : 'none';
iconPlay.style.display = playing ? 'none' : '';
btnLabel.textContent = playing ? 'Pause' : 'Resume';
statusEl.textContent = playing ? 'Playing' : 'Paused';
}
btn.addEventListener('click', function () {
playing ? slider.autoplay.stop() : slider.autoplay.start();
});
// Sync button state with intentional play/stop only
slider.on('autoplayStart', function () { playing = true; updateButton(); });
slider.on('autoplayStop', function () { playing = false; updateButton(); });