<div id="carousel-container"> <img src="image1.jpg"> <img src="image2.jpg"> <img src="image3.jpg"> </div>
#carousel-container { width: 500px; height: 300px; overflow: hidden; } #carousel-container img { width: 100%; height: 100%; object-fit: cover; }
function carousel() { var container = document.getElementById('carousel-container'); var images = container.getElementsByTagName('img'); var currentIndex = 0; setInterval(function() { images[currentIndex].style.display = 'none'; currentIndex = (currentIndex + 1) % images.length; images[currentIndex].style.display = 'block'; }, 2000); } window.onload = carousel;
<!DOCTYPE html> <html> <head> <title>轮播效果</title> <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript" src="script.js"></script> </head> <body> <div id="carousel-container"> <img src="image1.jpg"> <img src="image2.jpg"> <img src="image3.jpg"> </div> </body> </html>通过以上步骤,我们成功地实现了一个简单的动画效果的轮播。希望本文对于编程小白能够有所帮助!
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com