|
19 | 19 | height: 100%; |
20 | 20 | border-color: transparent; |
21 | 21 | } |
22 | | - |
23 | 22 | body { |
24 | 23 | background-color: transparent; |
25 | 24 | margin: auto; |
|
31 | 30 | <script src="images/images.js"></script> |
32 | 31 | <script src="settings.js"></script> |
33 | 32 | <script> |
34 | | - |
| 33 | + |
35 | 34 | // modes (defined in settings.js): |
36 | 35 | // 0: Random order (default) |
37 | 36 | // 1: Alphabetical order |
|
55 | 54 | images[i] = "images/" + images[i]; |
56 | 55 | indexes.push(i); |
57 | 56 | } |
58 | | - |
| 57 | + |
59 | 58 | // setup img elements |
60 | 59 | let topImage = document.createElement("img"); |
61 | 60 | let botImage = document.createElement("img"); |
|
64 | 63 | imageContainer.appendChild(topImage); |
65 | 64 | topImage.id = "topImage"; |
66 | 65 | botImage.id = "botImage"; |
67 | | - |
| 66 | + |
68 | 67 | // prevent white outline by setting initial transparency |
69 | 68 | topImage.style.opacity = "0.0"; |
70 | 69 | botImage.style.opacity = "0.0"; |
|
85 | 84 | function randomizeIndex(max) { |
86 | 85 | index = Math.floor(Math.random() * max); // [0, max) |
87 | 86 | } |
88 | | - |
89 | | - function nextSlide() { |
| 87 | + |
| 88 | + function update() { |
90 | 89 | 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; |
103 | 92 | } |
104 | | - |
105 | | - fadeInTop = !fadeInTop; |
| 93 | + getNextSlide(); |
106 | 94 | } |
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); |
140 | 100 | } |
| 101 | + |
| 102 | + let imageSrc = images[indexes[index]]; |
| 103 | + fadeInImage(imageSrc); |
141 | 104 | } |
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 | + |
143 | 126 | if (mode === 0) { // random mode |
144 | 127 | shuffle(indexes); |
145 | 128 | } else if (mode === 2) { // choose random image to start on |
146 | 129 | randomizeIndex(images.length); |
147 | 130 | } |
148 | 131 |
|
149 | 132 | // if only 1 image in slide show, fade in the image |
150 | | - if (images.length == 1) { |
| 133 | + if (images.length > 0) { |
151 | 134 | botImage.src = images[indexes[0]]; |
152 | 135 |
|
153 | 136 | $("#botImage").animate( |
154 | 137 | { opacity: 1.0 }, |
155 | 138 | { duration: fadeDuration } |
156 | 139 | ); |
| 140 | + } |
157 | 141 |
|
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); |
162 | 145 | } |
163 | 146 | </script> |
0 commit comments