forked from pixel-museum/css-art-museum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
157 lines (135 loc) Β· 5.44 KB
/
script.js
File metadata and controls
157 lines (135 loc) Β· 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
document.addEventListener('DOMContentLoaded', () => {
const galleryContainer = document.getElementById('gallery-container');
const searchBar = document.getElementById('search-bar'); // Grab the search input
let allArts = []; // Store all arts for filtering
async function loadArts() {
try {
const response = await fetch('arts.json');
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
const arts = await response.json();
allArts = arts; // Store arts for filtering
renderArts(allArts); // Initial render
} catch (error) {
console.error('Could not load arts:', error);
galleryContainer.innerHTML = '<p class="error-message">Could not load the art gallery. Please try again later.</p>';
}
}
// Function to render given arts
function renderArts(arts) {
galleryContainer.innerHTML = '';
arts.forEach(art => {
const artCard = document.createElement('div');
artCard.className = 'art-card';
const filePath = `arts/${art.file}`;
artCard.innerHTML = `
<iframe src="${filePath}" title="${art.title}" loading="lazy" seamless></iframe>
<p>${art.title} by ${art.author}</p>
`;
const viewerButtonAnchor = document.createElement("a");
viewerButtonAnchor.classList.add("view-code");
viewerButtonAnchor.href = `art-viewer.html?file=${encodeURIComponent(art.file)}`;
const button = document.createElement("button");
button.textContent = "View Code";
viewerButtonAnchor.appendChild(button);
artCard.appendChild(viewerButtonAnchor);
galleryContainer.appendChild(artCard);
});
initializeCardAnimations(); // Reinitialize animations
}
// --- Search Filter ---
searchBar.addEventListener('input', () => {
const query = searchBar.value.toLowerCase().trim();
const filteredArts = allArts.filter(art =>
art.title.toLowerCase().includes(query) || art.author.toLowerCase().includes(query)
);
renderArts(filteredArts);
});
// --- Theme toggle and other existing functions ---
const toggleBtn = document.getElementById("themeToggle");
const body = document.body;
if (localStorage.getItem("theme") === "dark") {
body.classList.add("dark-theme");
toggleBtn.textContent = "βοΈ Light";
}
toggleBtn.addEventListener("click", () => {
const ripple = document.createElement('span');
ripple.style.cssText = `
position: absolute; border-radius: 50%; background: rgba(108, 99, 255, 0.3);
transform: scale(0); animation: ripple 0.6s linear;
left: 50%; top: 50%; width: 20px; height: 20px; margin: -10px 0 0 -10px;
`;
toggleBtn.appendChild(ripple);
setTimeout(() => ripple.remove(), 600);
body.classList.toggle("dark-theme");
if (body.classList.contains("dark-theme")) {
toggleBtn.textContent = "βοΈ Light";
localStorage.setItem("theme", "dark");
} else {
toggleBtn.textContent = "π Dark";
localStorage.setItem("theme", "light");
}
});
function initializeCardAnimations() {
const artCards = document.querySelectorAll(".art-card");
if (artCards.length === 0) return;
const observerOptions = { threshold: 0.1, rootMargin: '0px 0px -50px 0px' };
const cardObserver = new IntersectionObserver((entries) => {
entries.forEach((entry, index) => {
if (entry.isIntersecting) {
setTimeout(() => { entry.target.style.animation = 'cardEntrance 0.8s ease-out both'; }, index * 100);
}
});
}, observerOptions);
artCards.forEach(card => {
cardObserver.observe(card);
card.addEventListener('mousemove', (e) => {
const rect = card.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
const rotateX = (e.clientY - centerY) / 20;
const rotateY = (centerX - e.clientX) / 20;
card.style.transform = `translateY(-12px) rotateX(${rotateX}deg) rotateY(${rotateY}deg)`;
});
card.addEventListener('mouseleave', () => { card.style.transform = ''; });
});
}
// Parallax
let ticking = false;
function updateParallax() {
const scrolled = window.pageYOffset;
requestAnimationFrame(() => {
document.body.style.backgroundPositionY = `${scrolled * 0.2}px`;
ticking = false;
});
}
window.addEventListener('scroll', () => { if (!ticking) { ticking = true; updateParallax(); } });
const style = document.createElement('style');
style.textContent = `
@keyframes ripple { to { transform: scale(4); opacity: 0; } }
.theme-toggle { position: relative; overflow: hidden; }
.error-message { text-align: center; color: #ff4d4d; grid-column: 1 / -1; }
`;
document.head.appendChild(style);
// Scroll to Top Button Functionality
const scrollToTopBtn = document.getElementById('scrollToTop');
const scrollThreshold = 300; // Show button after scrolling 300px
// Show/hide scroll to top button based on scroll position
function toggleScrollToTopButton() {
if (window.pageYOffset > scrollThreshold) {
scrollToTopBtn.classList.add('visible');
} else {
scrollToTopBtn.classList.remove('visible');
}
}
// Smooth scroll to top function
function scrollToTop() {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
}
// Add event listeners
window.addEventListener('scroll', toggleScrollToTopButton);
scrollToTopBtn.addEventListener('click', scrollToTop);
loadArts();
});