萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JavaScript + CSS 圖片輪播:自動反向切換、淡入淡出、指示器同步

自製輕量級圖片輪播:自動播放、滑鼠懸停暫停、指示器點擊切換、到達兩端自動反向、淡入淡出過渡。無第三方依賴,適合簡單專案或教學參考。

核心功能

  • 5 張圖片循環播放
  • 正向播放至最後一張 → 自動反向播放至第一張 → 循環
  • 指示器同步高亮,支援點擊跳轉
  • 滑鼠進入容器暫停,離開恢復
  • 淡入淡出動畫(CSS `opacity` + `transition`)

完整實作

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>自動反向圖片輪播</title>
<style>
  * { box-sizing:border-box; }
  body { background:#000; text-align:center; font:12px/20px Arial; padding:20px; }
  #slider { position:relative; width:320px; height:240px; margin:0 auto; border:8px solid #fff; border-radius:8px; background:#fff; box-shadow:0 4px 20px rgba(0,0,0,0.3); }
  .slides { position:relative; width:100%; height:100%; overflow:hidden; border:1px solid #ccc; }
  .slide { position:absolute; top:0; left:0; width:100%; height:100%; opacity:0; transition:opacity 0.3s ease; }
  .slide.active { opacity:1; }
  .slide img { width:100%; height:100%; object-fit:cover; display:block; }
  .indicators { position:absolute; right:10px; bottom:10px; display:flex; gap:6px; }
  .indicators button { width:24px; height:24px; border-radius:50%; border:none; background:#f90; color:#fff; opacity:0.7; cursor:pointer; font-weight:bold; transition:all 0.2s; }
  .indicators button.active { opacity:1; background:#f60; transform:scale(1.1); }
</style>
</head>
<body>
<div id="slider">
  <div class="slides">
    <div class="slide active"><img src="https://via.placeholder.com/320x240/007bff/fff?text=1" alt="Slide 1"></div>
    <div class="slide"><img src="https://via.placeholder.com/320x240/28a745/fff?text=2" alt="Slide 2"></div>
    <div class="slide"><img src="https://via.placeholder.com/320x240/dc3545/fff?text=3" alt="Slide 3"></div>
    <div class="slide"><img src="https://via.placeholder.com/320x240/ffc107/000?text=4" alt="Slide 4"></div>
    <div class="slide"><img src="https://via.placeholder.com/320x240/6f42c1/fff?text=5" alt="Slide 5"></div>
  </div>
  <div class="indicators" id="indicators"></div>
</div>

<script>
(function() {
  var slider = document.getElementById('slider');
  var slides = slider.querySelectorAll('.slide');
  var indicators = document.getElementById('indicators');
  var index = 0;
  var direction = 1; // 1: forward, -1: backward
  var timer = null;
  var animating = false;

  // 建立指示器按鈕
  slides.forEach(function(_, i) {
    var btn = document.createElement('button');
    btn.textContent = i + 1;
    btn.dataset.index = i;
    btn.addEventListener('click', function() { goTo(i); });
    indicators.appendChild(btn);
  });
  var indBtns = indicators.querySelectorAll('button');

  function updateUI() {
    slides.forEach(function(s, i) { s.classList.toggle('active', i === index); });
    indBtns.forEach(function(b, i) { b.classList.toggle('active', i === index); });
  }

  function goTo(i) {
    if (animating || i === index) return;
    animating = true;
    index = i;
    updateUI();
    setTimeout(function() { animating = false; }, 300); // 配合 CSS transition
  }

  function next() {
    var nextIndex = index + direction;
    if (nextIndex >= slides.length) { nextIndex = slides.length - 2; direction = -1; }
    else if (nextIndex < 0) { nextIndex = 1; direction = 1; }
    goTo(nextIndex);
  }

  function autoPlay() {
    timer = setInterval(next, 2000);
  }
  function stopAuto() { clearInterval(timer); }

  slider.addEventListener('mouseenter', stopAuto);
  slider.addEventListener('mouseleave', autoPlay);

  autoPlay();
})();
</script>
</body>
</html>

進階擴充

  • 觸控滑動支援(`touchstart`/`move`/`end` 計算位移)
  • 鍵盤左右鍵切換
  • 無障礙:`aria-roledescription="slide"`、`aria-label`、焦點管理
  • 響應式:CSS `aspect-ratio` 維持比例、斷點調整指示器佈局
  • 圖片懶載入:`loading="lazy"` 或 IntersectionObserver
copyright © 萬盛學電腦網 all rights reserved