Files
wordpress-theme-aksrt/assets/js/cms-home-modules.js
T

53 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* AKSRT CMS Home Modules
*
* - Tab 切换:[data-aksrt-tabs] / [data-tab-target] / [data-tab-id]
*/
(function () {
'use strict';
function initTabs(scope) {
const tablists = (scope || document).querySelectorAll('[data-aksrt-tabs]');
tablists.forEach(function (list) {
const buttons = list.querySelectorAll('.home-module__tab');
const section = list.closest('.home-module--tabs');
if (!section) return;
const panels = section.querySelectorAll('.home-module__tab-panel');
buttons.forEach(function (btn) {
btn.addEventListener('click', function () {
const target = btn.getAttribute('data-tab-target');
if (!target) return;
buttons.forEach(function (b) {
b.classList.toggle('is-active', b === btn);
b.setAttribute('aria-selected', b === btn ? 'true' : 'false');
});
panels.forEach(function (p) {
const isMatch = p.getAttribute('data-tab-id') === target;
p.classList.toggle('is-active', isMatch);
if (isMatch) {
p.removeAttribute('hidden');
} else {
p.setAttribute('hidden', '');
}
});
});
});
});
}
// 监听 postMessage:iframe 预览模式下接收 reload 请求
window.addEventListener('message', function (e) {
if (!e.data || e.data.type !== 'aksrt-preview-reload') return;
// 简单做:直接 reload
window.location.reload();
});
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function () { initTabs(); });
} else {
initTabs();
}
})();