1 回答

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
這就是我處理圖像淡入淡出的方式,好處是它直到圖像實(shí)際加載后才開始,所以淡入時(shí)它永遠(yuǎn)不會(huì)斷斷續(xù)續(xù)
JS
var image = document.querySelector('img');
var img = 1;
window.setInterval(changeImage,5000);
function changeImage() {
image.classList.remove('fadeIn')
image.src = 'img/'+img+'.jpg'
img++
if (img == 6) {
img = 1;
}
}
image.addEventListener('load', () => { // This function looks at the image and every time an image is loaded i.e whenever it gets a new src, it does a fade in animation
void(image.offsetHeight)
image.classList.add('fadeIn')
})
CSS
我通常用動(dòng)畫來做到這一點(diǎn),如下所示
#slideshow-home img {
width: 100%;
opacity: 0;
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.fadeIn {
animation:fadeIn 2s forwards;
}
添加回答
舉報(bào)