-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
102 lines (92 loc) · 3.94 KB
/
script.js
File metadata and controls
102 lines (92 loc) · 3.94 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
// Smooth scroll on click
document.querySelectorAll('header a').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({ behavior: 'smooth' });
}
});
});
document.querySelectorAll('.box').forEach(box => {
box.classList.add('expanded');
});
// Collapse/expand boxes only when clicking the title
document.querySelectorAll('.box-title').forEach(title => {
title.addEventListener('click', function(e) {
// Prevent click from bubbling up if there are nested clickable elements
e.stopPropagation();
this.parentElement.classList.toggle('collapsed');
this.parentElement.classList.toggle('expanded');
});
});
// Project card expansion functionality
document.addEventListener('DOMContentLoaded', function() {
const projectCards = document.querySelectorAll('.project-card');
// Show the back button when any card is expanded
function updateBackBtnVisibility() {
const anyExpanded = Array.from(projectCards).some(card => card.classList.contains('expanded') && card.classList.contains('active'));
backBtn.style.display = anyExpanded ? 'block' : 'none';
}
// Collapse all cards and scroll to top when back is clicked
backBtn.addEventListener('click', function() {
projectCards.forEach(card => {
card.classList.remove('expanded', 'active');
});
window.scrollTo({ top: 0, behavior: 'smooth' });
updateBackBtnVisibility();
document.getElementById('project-details').style.display = 'none';
document.getElementById('project-container').style.display = 'grid';
backBtn.style.display = 'none';
});
// Update back button visibility on card expand/collapse
projectCards.forEach(card => {
card.addEventListener('click', function(e) {
// Expand all cards
projectCards.forEach(c => c.classList.add('expanded', 'active'));
// Scroll the clicked card into view within the project-container
const container = card.closest('.column-large');
if (container) {
const cardRect = card.getBoundingClientRect();
const containerRect = container.getBoundingClientRect();
// Calculate the offset to center the card
const offset = cardRect.top - containerRect.top;
container.scrollBy({ top: offset, behavior: 'smooth' });
} else {
// fallback to default scrollIntoView
card.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
setTimeout(updateBackBtnVisibility, 100);
e.stopPropagation();
});
});
// Also update on Escape key
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
setTimeout(updateBackBtnVisibility, 100);
}
});
});
document.querySelectorAll('.project-card').forEach(card => {
card.addEventListener('click', function() {
document.getElementById('project-container').style.display = 'none';
document.getElementById('project-details').style.display = 'grid';
backBtn.style.display = 'block';
// Scroll to the corresponding project-card-details
const projectId = card.getAttribute('data-project');
const detail = document.querySelector('.project-card-details[data-project=\"' + projectId + '\"]');
if (detail) {
setTimeout(() => {
detail.scrollIntoView({ behavior: 'smooth', block: 'start' });
}, 100);
}
});
});
document.getElementById('project-details').addEventListener('click', function(e) {
// ถ้าคลิกที่ project-details โดยตรง (ไม่ใช่ลูกหลาน)
if (e.target === this) {
this.style.display = 'none';
document.getElementById('project-container').style.display = 'grid';
document.getElementById('back-btn').style.display = 'none';
}
});