以下是关于使用 JS 和 CSS 实现图片上下滚动的相关内容:
基础概念:
优势:
类型:
应用场景:
实现示例代码:
HTML 部分:
<div class="scroll-container">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
</div>
CSS 部分:
.scroll-container {
width: 500px;
height: 300px;
overflow: hidden;
position: relative;
}
.scroll-container img {
width: 100%;
height: auto;
position: absolute;
transition: top 1s ease-in-out;
}
JavaScript 部分:
const images = document.querySelectorAll('.scroll-container img');
let currentIndex = 0;
function scrollImages() {
images.forEach((img, index) => {
if (index !== currentIndex) {
img.style.top = '300px';
}
});
images[currentIndex].style.top = '0';
currentIndex = (currentIndex + 1) % images.length;
}
setInterval(scrollImages, 2000);
可能出现的问题及解决方法:
领取专属 10元无门槛券
手把手带您无忧上云