|
| 1 | +<!DOCTYPE html> |
1 | 2 | <!--
|
2 | 3 |
|
3 |
| -BrowserImageSlideshow v1.2 |
| 4 | +BrowserImageSlideshow |
4 | 5 | https://github.com/dustymethod/BrowserImageSlideshow
|
5 | 6 | https://obsproject.com/forum/threads/browser-image-slideshow.110157/
|
6 | 7 |
|
|
25 | 26 | overflow: hidden;
|
26 | 27 | }
|
27 | 28 | </style>
|
| 29 | + |
28 | 30 | <script src="jquery-3.4.1.min.js"></script>
|
29 | 31 | <script src="images/images.js"></script>
|
30 | 32 | <script src="settings.js"></script>
|
31 | 33 | <script>
|
32 |
| - function shuffle(a) { |
33 |
| - for (let i = a.length - 1; i > 0; i--) { |
34 |
| - const j = Math.floor(Math.random() * (i + 1)); |
35 |
| - [a[i], a[j]] = [a[j], a[i]]; |
36 |
| - } |
37 |
| - //console.log(a); |
38 |
| - return a; |
39 |
| - } |
40 |
| - |
41 |
| - // [0, max) |
42 |
| - function randomizeTick(max) { |
43 |
| - tick = Math.floor(Math.random() * max); |
44 |
| - } |
45 |
| - |
| 34 | + |
| 35 | + // modes (defined in settings.js): |
| 36 | + // 0: Random order (default) |
| 37 | + // 1: Alphabetical order |
| 38 | + // 2: Alphabetical order (start on random image) |
| 39 | + |
46 | 40 | // setup image src strings
|
47 | 41 | let images = imageNamesStr.split("\n");
|
48 | 42 | images.shift();
|
|
62 | 56 | indexes.push(i);
|
63 | 57 | }
|
64 | 58 |
|
65 |
| - // init img elements |
| 59 | + // setup img elements |
66 | 60 | let topImage = document.createElement("img");
|
67 | 61 | let botImage = document.createElement("img");
|
68 | 62 | let imageContainer = document.getElementById("imageContainer");
|
69 |
| - imageContainer.appendChild(topImage); |
70 | 63 | imageContainer.appendChild(botImage);
|
| 64 | + imageContainer.appendChild(topImage); |
71 | 65 | topImage.id = "topImage";
|
72 | 66 | botImage.id = "botImage";
|
73 | 67 |
|
74 | 68 | // prevent white outline by setting initial transparency
|
75 | 69 | topImage.style.opacity = "0.0";
|
76 | 70 | botImage.style.opacity = "0.0";
|
77 | 71 |
|
78 |
| - //set init image |
| 72 | + let fadeDuration = slideDuration * 0.25; |
| 73 | + let slideshowFunc; |
79 | 74 | let tick = 0;
|
80 |
| - if (mode === 0) { |
81 |
| - shuffle(indexes); |
82 |
| - } |
| 75 | + let fadeInTop = true; // bool for deciding if top image fades in or out |
83 | 76 |
|
84 |
| - //randomize tick |
85 |
| - if (mode === 2) { |
86 |
| - randomizeTick(images.length); |
| 77 | + function shuffle(a) { |
| 78 | + for (let i = a.length - 1; i > 0; i--) { |
| 79 | + const j = Math.floor(Math.random() * (i + 1)); |
| 80 | + [a[i], a[j]] = [a[j], a[i]]; |
| 81 | + } |
| 82 | + return a; |
87 | 83 | }
|
88 | 84 |
|
89 |
| - let fadeDuration = slideDuration * 0.25; |
90 |
| - let slideshowFunc; |
91 |
| - |
92 |
| - function slideshow() { |
93 |
| - //let fadeDuration = 1000; |
94 |
| - |
95 |
| - $("#botImage").animate({ |
96 |
| - opacity:1.0 |
97 |
| - }, |
98 |
| - { |
99 |
| - duration:fadeDuration |
100 |
| - }); |
101 |
| - |
102 |
| - $("#topImage").animate({ //properties |
103 |
| - opacity:0 |
104 |
| - }, |
105 |
| - { //options |
106 |
| - duration: fadeDuration, |
107 |
| - done: function(){ |
108 |
| - if (tick === images.length-1 && stopOnLastImage === true) { |
109 |
| - clearInterval(slideshowFunc); //stop slideshow on last slide |
110 |
| - } else { |
111 |
| - if (tick === images.length-1) { //reset |
112 |
| - if (mode === 0) { |
113 |
| - shuffle(indexes); |
114 |
| - } else if (mode === 2) { |
115 |
| - randomizeTick(images.length); |
116 |
| - } |
117 |
| - tick = 0; |
118 |
| - } else { |
119 |
| - tick++; |
120 |
| - } |
121 |
| - } |
| 85 | + function randomizeTick(max) { |
| 86 | + tick = Math.floor(Math.random() * max); // [0, max) |
| 87 | + } |
122 | 88 |
|
123 |
| - topImage.src = botImage.src; |
124 |
| - topImage.style.opacity = "1.0"; |
125 |
| - botImage.src = images[indexes[tick]]; |
126 |
| - botImage.style.opacity = "0.0"; |
| 89 | + function nextSlide() { |
| 90 | + if (tick === images.length-1 && stopOnLastImage === true) { |
| 91 | + clearInterval(slideshowFunc); // stop slideshow on last slide |
| 92 | + } else { |
| 93 | + if (tick === images.length-1) { // if end of loop, start new loop |
| 94 | + if (mode === 0) { |
| 95 | + shuffle(indexes); |
| 96 | + } else if (mode === 2) { |
| 97 | + randomizeTick(images.length); |
127 | 98 | }
|
| 99 | + tick = 0; |
| 100 | + } else { // next slide |
| 101 | + tick++; |
128 | 102 | }
|
129 |
| - ); |
| 103 | + } |
| 104 | + |
| 105 | + fadeInTop = !fadeInTop; |
130 | 106 | }
|
| 107 | + |
| 108 | + function slideshow() { |
| 109 | + if (fadeInTop) { |
| 110 | + topImage.src = images[indexes[tick]]; |
| 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 | + ); |
131 | 124 |
|
132 |
| - if (images.length > 0) { |
133 |
| - botImage.src = images[indexes[tick]]; |
134 |
| - botImage.style.opacity = "0.0"; |
135 |
| - |
136 |
| - //initial fade in |
137 |
| - $("#botImage").animate({ |
138 |
| - opacity:1.0 |
139 |
| - }, { duration:fadeDuration } |
140 |
| - ); |
| 125 | + } else { |
| 126 | + botImage.src = images[indexes[tick]]; |
| 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 | + ); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + if (mode === 0) { // random mode |
| 144 | + shuffle(indexes); |
| 145 | + } else if (mode === 2) { // choose random image to start on |
| 146 | + randomizeTick(images.length); |
| 147 | + } |
| 148 | + |
| 149 | + // if only 1 image in slide show, fade in the image |
| 150 | + if (images.length == 1) { |
| 151 | + botImage.src = images[indexes[0]]; |
141 | 152 |
|
142 |
| - $("#topImage").animate({ |
143 |
| - opacity:0 |
144 |
| - }, { duration:fadeDuration } |
| 153 | + $("#botImage").animate( |
| 154 | + { opacity: 1.0 }, |
| 155 | + { duration: fadeDuration } |
145 | 156 | );
|
146 | 157 |
|
147 |
| - if (images.length > 1) { |
148 |
| - slideshow(); |
149 |
| - slideshowFunc = setInterval(slideshow, slideDuration); |
150 |
| - } |
| 158 | + // else start the slideshow |
| 159 | + } else if (images.length > 1) { |
| 160 | + slideshow(); |
| 161 | + slideshowFunc = setInterval(slideshow, slideDuration); |
151 | 162 | }
|
152 | 163 | </script>
|
0 commit comments