feat(lightbox): 接入全站图片预览
This commit is contained in:
1 parent
0985d0cb0a
commit
ddf7c37556
29 files changed
+1212
-1805
No files matched your search
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* AKSRT Page Template
|
||||
*
|
||||
* Handles template-scoped content interactions: lightbox,
|
||||
* expandable article bodies, and one-shot query cleanup banners.
|
||||
* Handles template-scoped content interactions: expandable article bodies
|
||||
* and one-shot query cleanup banners.
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
@@ -10,69 +10,6 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
function initLightbox() {
|
||||
var overlay = document.getElementById('lightbox-overlay');
|
||||
var content = document.querySelector(".wp-posts-content[data-aksrt-lightbox='1'], .entry-content[data-aksrt-lightbox='1']");
|
||||
if (!overlay || !content || overlay.dataset.aksrtLightboxBound === '1') return;
|
||||
|
||||
overlay.dataset.aksrtLightboxBound = '1';
|
||||
|
||||
var image = overlay.querySelector('.lightbox-img');
|
||||
var caption = overlay.querySelector('.lightbox-caption');
|
||||
var closeBtn = overlay.querySelector('.lightbox-close');
|
||||
|
||||
function close() {
|
||||
overlay.classList.remove('is-open');
|
||||
overlay.setAttribute('aria-hidden', 'true');
|
||||
document.body.style.overflow = '';
|
||||
if (image) {
|
||||
image.src = '';
|
||||
image.alt = '';
|
||||
}
|
||||
}
|
||||
|
||||
content.querySelectorAll('img').forEach(function (img) {
|
||||
var link = img.closest('a[href]');
|
||||
var linkHref = link ? link.href : '';
|
||||
var linksToImage = /\.(?:avif|gif|jpe?g|png|svg|webp)(?:[?#].*)?$/i.test(linkHref);
|
||||
if (img.closest('[data-no-lightbox]') || (link && !linksToImage)) return;
|
||||
|
||||
img.classList.add('aksrt-lightbox-target');
|
||||
img.addEventListener('click', function (event) {
|
||||
if (!image) return;
|
||||
if (linksToImage) event.preventDefault();
|
||||
|
||||
image.src = linksToImage
|
||||
? linkHref
|
||||
: (img.getAttribute('data-src') || img.getAttribute('data-lazy-src') || img.currentSrc || img.src);
|
||||
image.alt = img.alt || '';
|
||||
|
||||
if (caption) {
|
||||
var figure = img.closest('figure');
|
||||
var figureCaption = figure ? figure.querySelector('figcaption') : null;
|
||||
caption.textContent = img.alt || (figureCaption ? figureCaption.textContent : '') || '';
|
||||
}
|
||||
|
||||
overlay.classList.add('is-open');
|
||||
overlay.setAttribute('aria-hidden', 'false');
|
||||
document.body.style.overflow = 'hidden';
|
||||
if (closeBtn) closeBtn.focus();
|
||||
});
|
||||
});
|
||||
|
||||
overlay.addEventListener('click', function (event) {
|
||||
if (event.target === overlay || event.target === closeBtn || (event.target.closest && event.target.closest('.lightbox-close'))) {
|
||||
close();
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener('keydown', function (event) {
|
||||
if (event.key === 'Escape' && overlay.classList.contains('is-open')) {
|
||||
close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function initContentExpand() {
|
||||
// legacy-style: inject .read-more inside .limit-height and expand on click.
|
||||
document.querySelectorAll('.wp-posts-content.limit-height, .limit-height[data-nav]').forEach(function (content) {
|
||||
@@ -150,7 +87,6 @@
|
||||
}
|
||||
|
||||
function init() {
|
||||
initLightbox();
|
||||
initContentExpand();
|
||||
initFlashQueryCleanup();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user