进行了一些小优化
This commit is contained in:
1 parent
10b3e6dd9f
commit
f5c60cd425
5 files changed
+127
-24
No files matched your search
@@ -27,6 +27,79 @@
|
||||
tabs[next].click();
|
||||
}
|
||||
|
||||
function initSwiperA11y(el) {
|
||||
if (el.dataset.autoplay !== '1' || !once(el, 'SwiperA11y')) return;
|
||||
var i18n = config.i18n || {};
|
||||
var userPaused = reducedMotion;
|
||||
var focusPaused = false;
|
||||
var button = document.createElement('button');
|
||||
button.type = 'button';
|
||||
button.className = 'aksrt-widget-swiper__pause';
|
||||
|
||||
function setControlState() {
|
||||
button.setAttribute('aria-pressed', userPaused ? 'true' : 'false');
|
||||
button.setAttribute('aria-label', userPaused ? (i18n.resume || '继续自动播放') : (i18n.pause || '暂停自动播放'));
|
||||
button.textContent = userPaused ? '▶' : 'Ⅱ';
|
||||
}
|
||||
|
||||
function autoplay() {
|
||||
return el.swiper && el.swiper.autoplay ? el.swiper.autoplay : null;
|
||||
}
|
||||
|
||||
function stopAutoplay() {
|
||||
var runtime = autoplay();
|
||||
if (runtime && typeof runtime.stop === 'function') runtime.stop();
|
||||
setControlState();
|
||||
}
|
||||
|
||||
function pause() {
|
||||
focusPaused = true;
|
||||
stopAutoplay();
|
||||
}
|
||||
|
||||
function resume() {
|
||||
if (reducedMotion || userPaused || focusPaused) {
|
||||
stopAutoplay();
|
||||
return;
|
||||
}
|
||||
var runtime = autoplay();
|
||||
if (runtime && typeof runtime.start === 'function') runtime.start();
|
||||
setControlState();
|
||||
}
|
||||
|
||||
button.addEventListener('click', function () {
|
||||
if (userPaused && !reducedMotion) {
|
||||
userPaused = false;
|
||||
resume();
|
||||
} else {
|
||||
userPaused = true;
|
||||
stopAutoplay();
|
||||
}
|
||||
});
|
||||
el.addEventListener('focusin', pause);
|
||||
el.addEventListener('focusout', function () {
|
||||
window.setTimeout(function () {
|
||||
if (el.contains(document.activeElement)) return;
|
||||
focusPaused = false;
|
||||
resume();
|
||||
}, 0);
|
||||
});
|
||||
|
||||
setControlState();
|
||||
el.appendChild(button);
|
||||
|
||||
var attempts = 0;
|
||||
function connectToSwiper() {
|
||||
if (autoplay()) {
|
||||
if (reducedMotion || userPaused || focusPaused) stopAutoplay();
|
||||
return;
|
||||
}
|
||||
attempts += 1;
|
||||
if (attempts < 120) window.requestAnimationFrame(connectToSwiper);
|
||||
}
|
||||
connectToSwiper();
|
||||
}
|
||||
|
||||
function videoType(url) {
|
||||
if (/\.m3u8(?:\?|$)/i.test(url)) return 'hls';
|
||||
if (/\.flv(?:\?|$)/i.test(url)) return 'flv';
|
||||
@@ -257,6 +330,8 @@
|
||||
|
||||
function scan(root) {
|
||||
var scope = root || document;
|
||||
if (scope.matches && scope.matches('.aksrt-widget-swiper')) initSwiperA11y(scope);
|
||||
scope.querySelectorAll('.aksrt-widget-swiper').forEach(initSwiperA11y);
|
||||
scope.querySelectorAll('.aksrt-widget-video[data-video-url]').forEach(initVideo);
|
||||
scope.querySelectorAll('.aksrt-widget-toc').forEach(initToc);
|
||||
scope.querySelectorAll('.aksrt-widget-quote').forEach(initQuote);
|
||||
|
||||
Reference in new issue
Block a user