You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
182 lines
4.4 KiB
182 lines
4.4 KiB
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Optimized Media Gallery</title>
|
|
<style>
|
|
html, body {
|
|
margin: 0;
|
|
padding: 0;
|
|
overflow-x: hidden; /* 禁止左右滚动 */
|
|
font-family: Arial, sans-serif;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
flex-direction: column;
|
|
}
|
|
.gallery {
|
|
display: grid;
|
|
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
|
|
gap: 10px;
|
|
width: 100%;
|
|
max-width: 100%;
|
|
margin: 20px 0;
|
|
padding: 0 10px;
|
|
}
|
|
.media {
|
|
position: relative;
|
|
overflow: hidden;
|
|
border-radius: 10px;
|
|
background-color: #f0f0f0;
|
|
aspect-ratio: 1 / 1;
|
|
}
|
|
.media img,
|
|
.media video {
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: cover;
|
|
border-radius: 10px;
|
|
cursor: pointer;
|
|
opacity: 0;
|
|
transition: opacity 0.3s, transform 0.3s;
|
|
}
|
|
.media img.loaded,
|
|
.media video.loaded {
|
|
opacity: 1;
|
|
}
|
|
.media img:hover,
|
|
.media video:hover {
|
|
transform: scale(1.05);
|
|
}
|
|
.modal {
|
|
display: none;
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
background-color: rgba(0, 0, 0, 0.9);
|
|
justify-content: center;
|
|
align-items: center;
|
|
z-index: 1000;
|
|
touch-action: none;
|
|
}
|
|
.modal.active {
|
|
display: flex;
|
|
}
|
|
.modal-content {
|
|
max-width: 90vw;
|
|
max-height: 90vh;
|
|
position: relative;
|
|
}
|
|
.modal-content img,
|
|
.modal-content video {
|
|
max-width: 100%;
|
|
max-height: 100%;
|
|
border-radius: 10px;
|
|
display: block;
|
|
}
|
|
body.modal-open {
|
|
overflow: hidden;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="gallery">
|
|
{% for file in files %}
|
|
<div class="media">
|
|
{% if file.endswith(('.jpg', '.jpeg', '.png', '.gif', '.JPG', '.JPEG', '.PNG', '.GIF')) %}
|
|
<img data-src="/media/{{ file }}" alt="{{ file }}" onclick="openModal({{ loop.index0 }})">
|
|
{% elif file.endswith(('.mp4', '.avi', '.mov', '.wmv', '.m4v', '.mpg')) %}
|
|
<video data-src="/media/{{ file }}" controls onclick="openModal({{ loop.index0 }})"></video>
|
|
{% endif %}
|
|
</div>
|
|
{% endfor %}
|
|
</div>
|
|
|
|
<!-- 模态框 -->
|
|
<div id="modal" class="modal">
|
|
<div class="modal-content" id="modal-content">
|
|
<img id="modal-image" src="" alt="Full view" />
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const mediaElements = document.querySelectorAll('.media img, .media video');
|
|
const modal = document.getElementById('modal');
|
|
const modalImage = document.getElementById('modal-image');
|
|
const modalContent = document.getElementById('modal-content');
|
|
const body = document.body;
|
|
|
|
let currentIndex = 0;
|
|
const imageSources = [];
|
|
|
|
// 收集图片路径
|
|
mediaElements.forEach(el => {
|
|
if (el.tagName.toLowerCase() === 'img') {
|
|
imageSources.push(el.dataset.src);
|
|
}
|
|
});
|
|
|
|
// 懒加载
|
|
const lazyLoadObserver = new IntersectionObserver((entries, observer) => {
|
|
entries.forEach(entry => {
|
|
if (entry.isIntersecting) {
|
|
const el = entry.target;
|
|
el.src = el.dataset.src;
|
|
el.classList.add('loaded');
|
|
observer.unobserve(el);
|
|
}
|
|
});
|
|
}, { rootMargin: '0px 0px 200px 0px' });
|
|
|
|
mediaElements.forEach(el => lazyLoadObserver.observe(el));
|
|
|
|
// 打开模态框
|
|
function openModal(index) {
|
|
currentIndex = index;
|
|
modalImage.src = imageSources[currentIndex];
|
|
modal.classList.add('active');
|
|
body.classList.add('modal-open');
|
|
}
|
|
|
|
// 点击空白关闭模态框
|
|
modal.addEventListener('click', (e) => {
|
|
if (!modalContent.contains(e.target)) {
|
|
closeModal();
|
|
}
|
|
});
|
|
|
|
function closeModal() {
|
|
modal.classList.remove('active');
|
|
body.classList.remove('modal-open');
|
|
modalImage.src = '';
|
|
}
|
|
|
|
// 上下滑切换图片
|
|
let touchStartY = 0;
|
|
|
|
modal.addEventListener('touchstart', e => {
|
|
touchStartY = e.touches[0].clientY;
|
|
});
|
|
|
|
modal.addEventListener('touchend', e => {
|
|
const touchEndY = e.changedTouches[0].clientY;
|
|
const deltaY = touchEndY - touchStartY;
|
|
|
|
if (Math.abs(deltaY) > 50) {
|
|
if (deltaY < 0 && currentIndex < imageSources.length - 1) {
|
|
currentIndex++;
|
|
} else if (deltaY > 0 && currentIndex > 0) {
|
|
currentIndex--;
|
|
}
|
|
modalImage.src = imageSources[currentIndex];
|
|
}
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|