萬盛學電腦網

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

HTML5 + JavaScript 實現類 Flash 圖片輪播:Canvas/無 Canvas 双模式完整代碼

在 HTML5 普及前,圖片輪播常依賴 Flash。如今純前端技術即可實現平滑切換、縮略圖導航、左右箭頭控制、鼠標懸停暫停等完整交互。本文提供基於 **絕對定位 + z-index 切換** 的無 Canvas 方案(兼容 IE8+),並附帶核心 CSS/JS 邏輯解析,適合不願引入重型庫的輕量級項目。

HTML 結構

<div id="playImages" class="play">
    <ul class="big_pic">
        <div class="prev"></div>
        <div class="next"></div>
        <a class="mark_left" href="javascript:;"></a>
        <a class="mark_right" href="javascript:;"></a>
        <li style="z-index: 1"><img src="image/1.JPG"></li>
        <li><img src="image/2.JPG"></li>
        <li><img src="image/3.JPG"></li>
        <li><img src="image/4.JPG"></li>
        <li><img src="image/5.JPG"></li>
        <li><img src="image/6.JPG"></li>
    </ul>
    <div class="small_pic">
        <ul>
            <li><img src="image/1.JPG"></li>
            <li><img src="image/2.JPG"></li>
            <!-- 對應縮略圖 -->
        </ul>
    </div>
</div>

關鍵 CSS 佈局

.play { width: 600px; height: 550px; position: relative; border: 2px solid #000; }
.big_pic { width: 600px; height: 400px; position: relative; overflow: hidden; background: #fff; }
.big_pic li { width: 600px; height: 400px; position: absolute; left: 0; top: 0; z-index: 0; display: none; }
.big_pic li:first-child { display: block; } /* 首張顯示 */
.big_pic li img { width: 100%; height: 100%; }

/* 左右點擊熱區 */
.mark_left, .mark_right { width: 300px; height: 400px; position: absolute; top: 0; z-index: 3000; opacity: 0; }
.mark_left { left: 0; background: orange; }
.mark_right { left: 300px; background: cornflowerblue; }

/* 箭頭按鈕 */
.prev, .next { width: 60px; height: 60px; position: absolute; top: 170px; z-index: 3001; opacity: 0; cursor: pointer; background: url(btn.gif) no-repeat; }
.prev { left: 10px; background-position: 0 0; }
.next { right: 10px; background-position: 0 -60px; }

/* 縮略圖區 */
.small_pic { width: 594px; height: 144px; border: 3px solid paleturquoise; overflow: hidden; position: relative; }
.small_pic ul { width: 594px; height: 144px; position: absolute; left: 0; top: 0; }
.small_pic li { float: left; width: 194px; height: 140px; border: 2px solid paleturquoise; opacity: 0.6; cursor: pointer; }
.small_pic li.active { opacity: 1; border-color: #f00; } /* 當前態 */
.small_pic li img { width: 100%; height: 100%; }

核心 JS 邏輯(精簡版)

var current = 0;
var $bigLi = $(".big_pic li");
var $smallLi = $(".small_pic li");
var timer;

function show(index) {
    $bigLi.stop(true, true).fadeOut(300).eq(index).fadeIn(300);
    $smallLi.removeClass("active").eq(index).addClass("active");
    current = index;
}

function next() { show((current + 1) % $bigLi.length); }
function prev() { show((current - 1 + $bigLi.length) % $bigLi.length); }

// 自動播放
function autoPlay() { timer = setInterval(next, 3000); }
autoPlay();

// 事件綁定
$(".prev, .mark_left").click(prev);
$(".next, .mark_right").click(next);
$smallLi.click(function() { show($(this).index()); });

// 懸停暫停
$(".play").hover(function() { clearInterval(timer); }, autoPlay);

進階:無縫循環滾動(縮略圖區)

縮略圖超出容器寬度時,需實現左右無縫滾動:

var $smallUl = $(".small_pic ul");
var liWidth = $smallLi.outerWidth(true);
var visibleCount = Math.floor($(".small_pic").width() / liWidth);

function scrollSmall(direction) { // direction: -1 左, 1 右
    var left = parseInt($smallUl.css("left")) || 0;
    var maxLeft = 0;
    var minLeft = -($smallLi.length - visibleCount) * liWidth;

    if (direction === -1 && left <= minLeft) {
        // 移動到末尾克隆區(需預先克隆前 visibleCount 項到末尾)
        $smallUl.css("left", maxLeft);
        left = maxLeft;
    } else if (direction === 1 && left >= maxLeft) {
        $smallUl.css("left", minLeft);
        left = minLeft;
    }
    $smallUl.stop().animate({ left: left - direction * liWidth }, 300);
}

原理:克隆首尾節點實現視覺無縫,到達邊界時瞬移回對應克隆區繼續動畫。

Canvas 版本思路(高性能/特效需求)

  1. 單 Canvas 繪製所有幀,drawImage 切換源圖坐標。
  2. 請求動畫幀 requestAnimationFrame 驅動過渡動畫(位移、縮放、旋轉、濾鏡)。
  3. 離屏 Canvas 預渲染縮略圖,減少主線程繪製開銷。
  4. WebGL 著色器實現 3D 翻轉、粒子爆炸等高級特效。

Canvas 版適合遊戲級、可視化大屏、極致 60fps 場景;DOM 版開發效率高、SEO 友好、無障礙支持好,足覆蓋 90% 業務需求。

最佳實踐检查清單

  • ✅ 圖片預加載(new Image().src),防止切換閃爍。
  • ✅ 響應式:容器百分比寬度,圖片 max-width:100%,JS 獲取實時尺寸計算。
  • ✅ 無障礙:role="region" aria-label="圖片輪播"、鍵盤左右鍵切換、aria-live 播報當前圖片。
  • ✅ 銷毀清理:組件卸載時 clearInterval、解綁事件、移除 DOM,防洩漏。

掌握 DOM 版輪播核心邏輯,不僅能應對日常業務,更能理解 Swiper、Slick 等成熟庫的內部原理,遇到定制需求時能夠舉一反三。

copyright © 萬盛學電腦網 all rights reserved