手动轮播图是一种常见的网页展示效果,通过使用HTML、CSS和JavaScript实现。以下是一个手动轮播图的实例教程,附带部分代码供参考:

  1. 在HTML中创建轮播图容器:
  1. 在CSS中设置轮播图样式:

    .carousel-container {

    width: 500px;

    height: 300px;

    position: relative;

    overflow: hidden;

    }

    .carousel-container img {

    width: 100%;

    height: 100%;

    position: absolute;

    top: 0;

    left: 0;

    opacity: 0; / 隐藏所有图片 /

    }

    .carousel-container img:first-child {

    opacity: 1; / 默认显示第一张图片 /

    }

  2. 使用JavaScript实现手动切换功能:

    var carouselContainer = document.querySelector(".carousel-container");

    var images = carouselContainer.querySelectorAll("img");

    var currentImageIndex = 0;

function showNextImage() {

images[currentImageIndex].style.opacity = 0; / 隐藏当前图片 /

currentImageIndex = (currentImageIndex + 1) % images.length;

images[currentImageIndex].style.opacity = 1; / 显示下一张图片 /

}

carouselContainer.addEventListener("click", showNextImage);