@@ -6,41 +6,114 @@ title: Gallery
6
6
nav : true
7
7
nav_order : 8
8
8
---
9
+
10
+
9
11
<div class =" carousel " style =" text-align : center ;" >
10
- <div style =" position : relative ; display : inline-block ; width : 80% ;" >
11
- <div class="carousel-slide">
12
- <img class="carousel-image" src="/assets/img/lab_photo.jpg" alt="Lab Photo">
13
- <div class="caption">KGML Team</div>
14
- </div>
15
-
16
- <div class="carousel-slide" style="display:none;">
17
- <img class="carousel-image" src="/assets/img/jie_grad_celebration.jpg" alt="Graduation Celebration">
18
- <div class="caption">Jie’s PhD Defense Celebration</div>
19
- </div>
20
-
12
+ <div style =" position : relative ; display : inline-block ; width : 80% ;" id =" carousel-container " >
21
13
<button class="prev" onclick="changeSlide(-1)">❮</button>
22
14
<button class="next" onclick="changeSlide(1)">❯</button>
23
15
</div >
16
+
17
+ <!-- Scrollable Thumbnail strip (centered) -->
18
+ <div id =" thumbnail-container-wrapper " style =" display : flex ; justify-content : center ; margin-top : 20px ;" >
19
+ <div id="thumbnail-container" style="display: inline-block; width: 80%; overflow-x: auto; white-space: nowrap; padding: 10px 0;">
20
+ </div>
21
+ </div >
24
22
</div >
25
23
26
24
<script >
27
25
let slideIndex = 0 ;
28
- showSlides (slideIndex);
26
+ let slides = [];
27
+ let autoSlideInterval;
28
+
29
+ // Function to load the JSON data
30
+ async function loadGallery () {
31
+ try {
32
+ const response = await fetch (' /assets/img/gallery/gallery.json?v=' + new Date ().getTime ());
33
+ const images = await response .json ();
34
+ createCarousel (images);
35
+ } catch (error) {
36
+ console .error (" Error loading gallery JSON:" , error);
37
+ }
38
+ }
29
39
40
+ // Function to dynamically generate carousel slides and thumbnails
41
+ function createCarousel (images ) {
42
+ const carouselContainer = document .getElementById (' carousel-container' );
43
+ const thumbnailContainer = document .getElementById (' thumbnail-container' );
44
+
45
+ images .forEach ((image , index ) => {
46
+ // Create the slide
47
+ const slideDiv = document .createElement (' div' );
48
+ slideDiv .classList .add (' carousel-slide' );
49
+ if (index !== 0 ) slideDiv .style .display = " none" ; // Only show the first image
50
+ slides .push (slideDiv); // Store slides in array
51
+
52
+ const img = document .createElement (' img' );
53
+ img .classList .add (' carousel-image' );
54
+ img .src = image .src ;
55
+ img .alt = image .caption ;
56
+ img .style .width = " 100%" ;
57
+
58
+ const caption = document .createElement (' div' );
59
+ caption .classList .add (' caption' );
60
+ caption .textContent = image .caption ;
61
+
62
+ slideDiv .appendChild (img);
63
+ slideDiv .appendChild (caption);
64
+ carouselContainer .insertBefore (slideDiv, carouselContainer .children [0 ]); // Add slides before buttons
65
+
66
+ // Create the thumbnail
67
+ const thumbnail = document .createElement (' img' );
68
+ thumbnail .src = image .src ;
69
+ thumbnail .alt = image .caption ;
70
+ thumbnail .classList .add (' thumbnail' );
71
+ thumbnail .onclick = () => {
72
+ showSlide (index); // Go to specific slide on click
73
+ clearInterval (autoSlideInterval); // Stop auto sliding when clicked
74
+ startAutoSlide (); // Restart auto sliding
75
+ };
76
+ thumbnailContainer .appendChild (thumbnail);
77
+ });
78
+
79
+ // Initialize first thumbnail as active
80
+ updateActiveThumbnail (slideIndex);
81
+ startAutoSlide (); // Start auto-sliding after creating the carousel
82
+ }
83
+
84
+ // Function to change slides (next/previous)
30
85
function changeSlide (n ) {
31
- showSlides (slideIndex += n);
86
+ slideIndex += n;
87
+ if (slideIndex >= slides .length ) { slideIndex = 0 }
88
+ if (slideIndex < 0 ) { slideIndex = slides .length - 1 }
89
+ showSlide (slideIndex);
32
90
}
33
91
34
- function showSlides (n ) {
35
- let i;
36
- let slides = document .getElementsByClassName (" carousel-slide" );
37
- if (n >= slides .length ) { slideIndex = 0 }
38
- if (n < 0 ) { slideIndex = slides .length - 1 }
39
- for (i = 0 ; i < slides .length ; i++ ) {
40
- slides[i].style .display = " none" ;
41
- }
42
- slides[slideIndex].style .display = " block" ;
92
+ // Function to show a specific slide and update active thumbnail
93
+ function showSlide (n ) {
94
+ slides .forEach (slide => slide .style .display = " none" ); // Hide all slides
95
+ slides[n].style .display = " block" ; // Show the selected slide
96
+ slideIndex = n;
97
+ updateActiveThumbnail (n); // Update thumbnail highlight
43
98
}
99
+
100
+ // Function to update the active thumbnail
101
+ function updateActiveThumbnail (index ) {
102
+ const thumbnails = document .querySelectorAll (' .thumbnail' );
103
+ thumbnails .forEach (thumb => thumb .classList .remove (' active' )); // Remove active class from all
104
+ thumbnails[index].classList .add (' active' ); // Add active class to current thumbnail
105
+ }
106
+
107
+ // Function to start auto-sliding every 3 seconds
108
+ function startAutoSlide () {
109
+ autoSlideInterval = setInterval (() => {
110
+ slideIndex = (slideIndex + 1 ) % slides .length ; // Automatically move to the next slide
111
+ showSlide (slideIndex);
112
+ }, 3000 ); // 3000ms = 3 seconds
113
+ }
114
+
115
+ // Load the gallery when the page loads
116
+ loadGallery ();
44
117
</script >
45
118
46
119
<style >
@@ -70,11 +143,11 @@ nav_order: 8
70
143
}
71
144
72
145
.prev {
73
- left : 10px ; /* Adjusted to position the arrow inside the 90% container */
146
+ left : 10px ;
74
147
}
75
148
76
149
.next {
77
- right : 10px ; /* Adjusted to position the arrow inside the 90% container */
150
+ right : 10px ;
78
151
}
79
152
80
153
.caption {
@@ -88,4 +161,38 @@ nav_order: 8
88
161
.carousel-slide {
89
162
position : relative ;
90
163
}
164
+
165
+ /* Thumbnails */
166
+ .thumbnail {
167
+ width : 80px ;
168
+ height : auto ;
169
+ margin : 0 5px ;
170
+ cursor : pointer ;
171
+ opacity : 0.6 ;
172
+ transition : opacity 0.3s ease ;
173
+ display : inline-block ;
174
+ }
175
+
176
+ .thumbnail :hover {
177
+ opacity : 1 ;
178
+ }
179
+
180
+ .thumbnail.active {
181
+ border : 2px solid #333 ; /* Highlight the active thumbnail */
182
+ opacity : 1 ; /* Fully visible */
183
+ }
184
+
185
+ /* Scrollbar styling (optional) */
186
+ #thumbnail-container ::-webkit-scrollbar {
187
+ height : 8px ;
188
+ }
189
+
190
+ #thumbnail-container ::-webkit-scrollbar-thumb {
191
+ background-color : #888 ;
192
+ border-radius : 10px ;
193
+ }
194
+
195
+ #thumbnail-container ::-webkit-scrollbar-thumb :hover {
196
+ background-color : #555 ;
197
+ }
91
198
</style >
0 commit comments