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.

212 lines
5.0 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SSSSS</title>
<style>
html, body {
margin: 0;
padding: 0;
overflow-x: hidden;
font-family: Arial, sans-serif;
touch-action: manipulation;
}
body.modal-open {
overflow: hidden;
position: fixed;
width: 100%;
top: 0;
}
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 10px;
width: 100%;
max-width: 100vw;
margin: 20px 0;
padding: 0 10px;
box-sizing: border-box;
}
.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: 100vw;
height: 100vh;
background-color: rgba(0, 0, 0, 0.9);
justify-content: center;
align-items: center;
z-index: 1000;
flex-direction: column;
touch-action: none;
}
.modal.active {
display: flex;
}
.modal-content {
position: relative;
max-width: 100vw;
max-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
box-sizing: border-box;
padding: 10px;
}
.modal-content img,
.modal-content video {
max-width: 100vw;
max-height: 100vh;
width: auto;
height: auto;
object-fit: contain;
border-radius: 10px;
display: block;
}
</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('{{ file }}', 'image')">
{% elif file.endswith(('.mp4', '.avi', '.mov', '.wmv', '.m4v', '.mpg')) %}
<video data-src="/media/{{ file }}" muted playsinline onclick="openModal('{{ file }}', 'video')">
Your browser does not support the video tag.
</video>
{% endif %}
</div>
{% endfor %}
</div>
<!-- 模态框 -->
<div id="modal" class="modal">
<div class="modal-content" id="modalContent"></div>
</div>
<script>
const mediaElements = document.querySelectorAll('.media img, .media video');
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));
const modal = document.getElementById('modal');
const modalContent = document.getElementById('modalContent');
const body = document.body;
let scrollY = 0;
let files = [{% for file in files %}"{{ file }}",{% endfor %}];
let currentIndex = 0;
function openModal(file, type) {
currentIndex = files.indexOf(file);
showModal(file, type);
scrollY = window.scrollY;
body.classList.add('modal-open');
body.style.top = `-${scrollY}px`;
modal.classList.add('active');
}
function closeModal() {
modal.classList.remove('active');
body.classList.remove('modal-open');
modalContent.innerHTML = '';
window.scrollTo(0, scrollY);
}
function showModal(file, type) {
modalContent.innerHTML = '';
const media = document.createElement(type === 'image' ? 'img' : 'video');
media.src = `/media/${file}`;
if (type === 'video') {
media.controls = true;
media.muted = true;
media.autoplay = true;
media.playsInline = true;
media.loop = true;
}
modalContent.appendChild(media);
}
modal.addEventListener('click', (e) => {
if (!modalContent.contains(e.target)) {
closeModal();
}
});
// 滑动切换
let startY = 0;
modal.addEventListener('touchstart', e => {
startY = e.touches[0].clientY;
});
modal.addEventListener('touchend', e => {
const endY = e.changedTouches[0].clientY;
const deltaY = endY - startY;
if (Math.abs(deltaY) > 50) {
if (deltaY < 0) {
currentIndex = (currentIndex + 1) % files.length;
} else {
currentIndex = (currentIndex - 1 + files.length) % files.length;
}
const nextFile = files[currentIndex];
const ext = nextFile.split('.').pop().toLowerCase();
const isImage = ['jpg', 'jpeg', 'png', 'gif'].includes(ext);
showModal(nextFile, isImage ? 'image' : 'video');
}
});
</script>
</body>
</html>