1 回答

TA貢獻1851條經驗 獲得超4個贊
至于淡入,這需要對您的 html 進行一些調整。您需要有 2 張圖像 - 舊圖像和新圖像。
<img?height="170"?id="rotator-old"?class="img-fluid,?image-hover"?/> <img?height="170"?id="rotator-new"?class="img-fluid,?image-hover"?/>
然后在你的 js 中,你需要改變每個類,并應用 see 來淡化不透明度。在你的 js 中:
var rotatorOld = document.getElementById("rotator-old");?
var rotatorNew = document.getElementById("rotator-new");?
var imageDir = [<image urls>];
var shuffledImages = shuffle(imageDir)
var i = 0;
function loop() {
? // remove visibility from image being transitioned to
? rotatorNew.classList.remove('visible')
? if(i > (imageDir.length - 1)){
? ? i = 0;
? }
??
? if (i > 0) { // starting from rest with the first image
? ? // set image in the background to previous image
? ? rotatorOld.src = shuffledImages[i-1]
? }
? // set image in the foreground to new image
? rotatorNew.src = shuffledImages[i]
? // add visibility class with css transition
? rotatorNew.classList.add('visible')
? i++;
? loopTimer = setTimeout(loop ,3000);
}
loop();
在您的 CSS 中,在 2.5 秒延遲后以 0.5 秒過渡過渡不透明度:
#rotator-new {
? opacity: 0;
}
#rotator-new.visible {
? opacity: 1;
? transition: opacity 500ms 2500ms // or whatever
}
您還需要使用一些 CSS 來確保新舊圖像正確重疊。position: relative;這可以通過將父級設置為 have a并將每個子級img設置為 have來完成position: absolute; top: 0, bottom: 0; left: 0, right: 0;。
我沒有機會測試這段代碼,所以它可能需要一些調整才能正常運行。但是如果你用一些示例圖像制作一個 codesandbox 或 codepen,我相信我們可以讓它工作。如果不需要自己編寫代碼,我還建議尋找第 3 方庫來為您處理此問題。這稱為輪播,Boostrap是您可以使用的一個很好的輪播。
添加回答
舉報