Skip to content

Commit 040281c

Browse files
committed
Refactor logic
renamed slideshow() to update(), which is called each interval in setInterval transition logic moved to getNextSlide() and fadeInImage()
1 parent 9c218cf commit 040281c

1 file changed

Lines changed: 42 additions & 59 deletions

File tree

BrowserImageSlideshow.html

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
height: 100%;
2020
border-color: transparent;
2121
}
22-
2322
body {
2423
background-color: transparent;
2524
margin: auto;
@@ -31,7 +30,7 @@
3130
<script src="images/images.js"></script>
3231
<script src="settings.js"></script>
3332
<script>
34-
33+
3534
// modes (defined in settings.js):
3635
// 0: Random order (default)
3736
// 1: Alphabetical order
@@ -55,7 +54,7 @@
5554
images[i] = "images/" + images[i];
5655
indexes.push(i);
5756
}
58-
57+
5958
// setup img elements
6059
let topImage = document.createElement("img");
6160
let botImage = document.createElement("img");
@@ -64,7 +63,7 @@
6463
imageContainer.appendChild(topImage);
6564
topImage.id = "topImage";
6665
botImage.id = "botImage";
67-
66+
6867
// prevent white outline by setting initial transparency
6968
topImage.style.opacity = "0.0";
7069
botImage.style.opacity = "0.0";
@@ -85,79 +84,63 @@
8584
function randomizeIndex(max) {
8685
index = Math.floor(Math.random() * max); // [0, max)
8786
}
88-
89-
function nextSlide() {
87+
88+
function update() {
9089
if (index === images.length-1 && stopOnLastImage === true) {
91-
clearInterval(slideshowFunc); // stop slideshow on last slide
92-
} else {
93-
if (index === images.length-1) { // if end of loop, start new loop
94-
if (mode === 0) {
95-
shuffle(indexes);
96-
} else if (mode === 2) {
97-
randomizeIndex(images.length);
98-
}
99-
index = 0;
100-
} else { // next slide
101-
index++;
102-
}
90+
clearInterval(slideshowFunc);
91+
return;
10392
}
104-
105-
fadeInTop = !fadeInTop;
93+
getNextSlide();
10694
}
107-
108-
function slideshow() {
109-
if (fadeInTop) {
110-
topImage.src = images[indexes[index]];
111-
112-
$("#botImage").animate(
113-
{ opacity: 0.0 },
114-
{ duration: fadeDuration }
115-
);
116-
117-
$("#topImage").animate(
118-
{ opacity: 1.0 },
119-
{
120-
duration: fadeDuration,
121-
done: nextSlide
122-
}
123-
);
124-
125-
} else {
126-
botImage.src = images[indexes[index]];
127-
128-
$("#botImage").animate(
129-
{ opacity: 1.0 },
130-
{ duration: fadeDuration }
131-
);
132-
133-
$("#topImage").animate(
134-
{ opacity: 0.0 },
135-
{
136-
duration: fadeDuration,
137-
done: nextSlide
138-
}
139-
);
95+
96+
function getNextSlide() {
97+
index = index === images.length - 1 ? 0 : index + 1;
98+
if (index === 0 && mode === 0) {
99+
shuffle(indexes);
140100
}
101+
102+
let imageSrc = images[indexes[index]];
103+
fadeInImage(imageSrc);
141104
}
142-
105+
106+
function fadeInImage(imageSrc) {
107+
if (fadeInTop) { topImage.src = imageSrc; }
108+
else { botImage.src = imageSrc; }
109+
110+
const botTarget = fadeInTop ? 0.0 : 1.0;
111+
const topTarget = fadeInTop ? 1.0 : 0.0;
112+
113+
$("#botImage").animate(
114+
{ opacity: botTarget },
115+
{ duration: fadeDuration }
116+
);
117+
118+
$("#topImage").animate(
119+
{ opacity: topTarget },
120+
{ duration: fadeDuration }
121+
);
122+
123+
fadeInTop = !fadeInTop;
124+
}
125+
143126
if (mode === 0) { // random mode
144127
shuffle(indexes);
145128
} else if (mode === 2) { // choose random image to start on
146129
randomizeIndex(images.length);
147130
}
148131

149132
// if only 1 image in slide show, fade in the image
150-
if (images.length == 1) {
133+
if (images.length > 0) {
151134
botImage.src = images[indexes[0]];
152135

153136
$("#botImage").animate(
154137
{ opacity: 1.0 },
155138
{ duration: fadeDuration }
156139
);
140+
}
157141

158-
// else start the slideshow
159-
} else if (images.length > 1) {
160-
slideshow();
161-
slideshowFunc = setInterval(slideshow, slideDuration);
142+
// start slideshow
143+
if (images.length > 1) {
144+
slideshowFunc = setInterval(update, slideDuration);
162145
}
163146
</script>

0 commit comments

Comments
 (0)