1 回答
TA贡献1846条经验 获得超7个赞
这就是我处理图像淡入淡出的方式,好处是它直到图像实际加载后才开始,所以淡入时它永远不会断断续续
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
我通常用动画来做到这一点,如下所示
#slideshow-home img {
width: 100%;
opacity: 0;
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.fadeIn {
animation:fadeIn 2s forwards;
}
添加回答
举报