Javascript初心者です。スライドショーを作ろうとしているのですが、ウェブページをロードした際にはじめの画像が表示されるまでに少し時間がかかってしまいます。画像をはじめからウェブサイト上に表示させるにはどうすればよいのでしょうか。
こちらが自分のコードです。
https://jsfiddle.net/t0hfxkq3/3/
HTML
1<img id="slider" width="200" height="200" alt="slide">
CSS
1#slider { 2 opacity: 1; 3 transition: opacity 1s; 4} 5 6#slider.fadeOut { 7 opacity: 0; 8}
javascript
1var imgArray = [ 2 'https://images.unsplash.com/photo-1521673461164-de300ebcfb17?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60', 3 'https://images.unsplash.com/photo-1497993950456-cdb57afd1cf1?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60', 4 'https://images.unsplash.com/photo-1530281700549-e82e7bf110d6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60'], 5 curIndex = 0; 6 imgDuration = 3000; 7 8function slideShow() { 9 document.getElementById('slider').className += "fadeOut"; 10 setTimeout(function() { 11 document.getElementById('slider').src = imgArray[curIndex]; 12 document.getElementById('slider').className = ""; 13 },1000); 14 curIndex++; 15 if (curIndex == imgArray.length) { curIndex = 0; } 16 setTimeout(slideShow, imgDuration); 17} 18slideShow();
スライドショーを作るのにはこのウェブページの二つ目のコードを参考にしました。
https://stackoverflow.com/questions/25347946/add-fade-effect-in-slideshow-javascript

