萬盛學電腦網

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

JavaScript 實現同一視窗切換圖片預覽(點擊縮圖換大圖)

核心思路

點擊縮圖/連結 → 獲取其 href(大圖地址)與 title(描述) → 設置大圖 src 與描述文本 → 取消默認跳轉(return false)。

完整可運行示例

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>同頁圖片切換預覽</title>
  <style>
    .gallery { max-width: 600px; margin: 20px auto; font-family: sans-serif; }
    .thumbnails { display: flex; gap: 10px; margin-bottom: 15px; flex-wrap: wrap; justify-content: center; }
    .thumbnails a { display: block; border: 2px solid transparent; border-radius: 4px; transition: border-color .2s; text-decoration: none; color: inherit; }
    .thumbnails a:hover { border-color: #007bff; }
    .thumbnails img { width: 80px; height: 60px; object-fit: cover; border-radius: 2px; }
    .main-image { width: 100%; max-height: 400px; object-fit: contain; border: 1px solid #ddd; border-radius: 4px; background: #fafafa; }
    .caption { text-align: center; margin-top: 8px; color: #666; font-size: 14px; min-height: 1.4em; }
  </style>
</head>
<body>
  <div class="gallery">
    <div class="thumbnails">
      <a href="https://picsum.photos/id/101/800/600" title="風景 1" onclick="return showImage(this)">
        <img src="https://picsum.photos/id/101/160/120" alt="縮圖1">
      </a>
      <a href="https://picsum.photos/id/102/800/600" title="風景 2" onclick="return showImage(this)">
        <img src="https://picsum.photos/id/102/160/120" alt="縮圖2">
      </a>
      <a href="https://picsum.photos/id/103/800/600" title="風景 3" onclick="return showImage(this)">
        <img src="https://picsum.photos/id/103/160/120" alt="縮圖3">
      </a>
    </div>
    <img id="mainImg" class="main-image" src="https://picsum.photos/id/101/800/600" alt="大圖預覽">
    <p id="caption" class="caption">風景 1</p>
  </div>

  <script>
    function showImage(link) {
      var mainImg = document.getElementById('mainImg');
      var caption = document.getElementById('caption');
      
      // 獲取大圖地址與標題
      var src = link.getAttribute('href');
      var title = link.getAttribute('title') || '';
      
      // 切換圖片(可添加淡入效果)
      mainImg.style.opacity = '0';
      setTimeout(function() {
        mainImg.src = src;
        mainImg.onload = function() {
          mainImg.style.opacity = '1';
        };
      }, 100);
      
      caption.textContent = title;
      return false; // 阻止  默認跳轉
    }
  </script>
</body>
</html>

進階:jQuery 事件委派版(動態縮圖)

$('.thumbnails').on('click', 'a', function(e) {
  e.preventDefault();
  var $main = $('#mainImg');
  var src = this.href;
  var title = this.title;
  
  $main.fadeOut(150, function() {
    this.src = src;
    $(this).fadeIn(150);
  });
  $('#caption').text(title);
});

現代優化:預載入大圖

// 頁面載入後預載入所有大圖
$('.thumbnails a').each(function() {
  new Image().src = this.href;
});

// 或使用 IntersectionObserver 懶加載
const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      new Image().src = entry.target.href;
      observer.unobserve(entry.target);
    }
  });
});
document.querySelectorAll('.thumbnails a').forEach(a => observer.observe(a));

無障礙增強

  • 縮圖 a 添加 role="button"、aria-label="查看大圖: {title}"。
  • 大圖 img 保持 alt 描述。
  • 鍵盤可操作:Tab 聚焦縮圖,Enter/Space 觸發切換。

總結

同頁切換大圖核心極簡:href 存大圖地址,onclick 設置 src 並阻止跳轉。現代開發建議:事件委派 + fadeIn/Out 過渡 + 預載入/懶加載 + 無障礙屬性,構建流暢、無障礙的圖庫體驗。

copyright © 萬盛學電腦網 all rights reserved