icons-enhanced/js/scripts.js
Snowz 79b13fb280 feat: 新增App图标搜索器增强版功能
引入完整的App图标搜索器增强版,包括前端页面、后端缓存系统、样式和脚本。主要功能包括实时搜索、多尺寸图标下载、图片预览、响应式设计和智能缓存机制。后端通过ImageCache类实现图片缓存,前端通过JavaScript优化搜索交互和图片加载体验。新增README.md提供详细的部署和开发指南。
2025-04-12 15:38:09 +08:00

105 lines
3.3 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.

document.addEventListener('DOMContentLoaded', function() {
const backToTop = document.getElementById('back-to-top');
const modal = document.getElementById('imagePreview');
const modalImg = document.getElementById('previewImage');
const closeBtn = document.getElementsByClassName('close')[0];
const searchInput = document.getElementById('searchInput');
const iconGrid = document.getElementById('iconGrid');
// 返回顶部按钮
window.onscroll = function() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
backToTop.style.display = 'block';
} else {
backToTop.style.display = 'none';
}
};
backToTop.onclick = function() {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
};
// 图标预览模态框
document.querySelectorAll('.icon-image img').forEach(img => {
img.onclick = function() {
modal.style.display = 'block';
modalImg.src = this.getAttribute('data-high-res');
modalImg.alt = this.alt;
};
});
closeBtn.onclick = function() {
modal.style.display = 'none';
};
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = 'none';
}
};
// 搜索输入优化
// 搜索防抖功能
function debounce(func, wait) {
let timeout;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), wait);
};
}
document.addEventListener('DOMContentLoaded', function() {
const searchForm = document.getElementById('searchForm');
const searchInput = document.getElementById('searchInput');
// 防止表单自动提交
searchForm.addEventListener('submit', function(e) {
e.preventDefault();
if (searchInput.value.trim()) {
this.submit();
}
});
// 输入防抖
const debouncedSearch = debounce(function(value) {
if (value.trim().length >= 2) {
searchForm.submit();
}
}, 800);
// 监听输入事件
searchInput.addEventListener('input', function(e) {
const value = e.target.value;
debouncedSearch(value);
});
// 添加加载状态
searchForm.addEventListener('submit', function() {
const button = this.querySelector('button[type="submit"]');
button.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
button.disabled = true;
// 2秒后恢复按钮状态如果请求完成会被新页面覆盖
setTimeout(() => {
button.innerHTML = '<i class="fas fa-search"></i>';
button.disabled = false;
}, 2000);
});
});
// 图标加载动画
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('fade-in');
}
});
});
document.querySelectorAll('.icon-card').forEach(card => {
observer.observe(card);
});
});