萬盛學電腦網

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

jQuery Orbit.js 圖片折疊輪播特效完整整合教學

jQuery Orbit.js 圖片輪播插件整合與客製化完整指南

Orbit.js 是 ZURB Foundation 框架內建的響應式圖片輪播插件,支援折疊/立體切換效果。本文詳細說明整合步驟、配置選項、常見問題與現代替代方案。

整合步驟詳解

1. 檔案結構準備


project/
├── css/
│   ├── chuxz.css          # Orbit 核心樣式
│   └── orbit.css          # 可選:主題樣式
├── js/
│   ├── jquery.min.js      # jQuery 1.7+ (必須)
│   ├── jq.orbit.js        # Orbit 核心邏輯
│   └── orbit.js           # 初始化配置
└── images/
    ├── 1.jpg ~ 5.jpg      # 輪播圖片
    └── loading.gif        # 載入指示器

2. HTML 結構規範


<div class="orbit-wrapper">
  <div id="featured" class="orbit" role="region" aria-label="圖片輪播">
    <a href="https://example.com/1">
      <img src="images/1.jpg" width="185" alt="《Just Married》" title="《Just Married》">
    </a>
    <a href="https://example.com/2">
      <img src="images/2.jpg" width="185" alt="《佩加索斯》" title="《佩加索斯》">
    </a>
    <a href="https://example.com/3">
      <img src="images/3.jpg" width="185" alt="《太廟的一場偶遇》" title="《太廟的一場偶遇》">
    </a>
    <a href="https://example.com/4">
      <img src="images/4.jpg" width="185" alt="《為愛癡狂》" title="《為愛癡狂》">
    </a>
    <a href="https://example.com/5">
      <img src="images/5.jpg" width="185" alt="《游走,在北京的每一條街》" title="《游走,在北京的每一條街》">
    </a>
  </div>
</div>

<!-- 載入腳本 (建議置底) -->
<script src="js/jquery.min.js"></script>
<script src="js/jq.orbit.js"></script>
<script src="js/orbit.js"></script>

3. orbit.js 初始化配置範例


$(window).on('load', function() {
  $('#featured').orbit({
    // 基本設定
    animation: 'fade',        // fade | horizontal-slide | vertical-slide | horizontal-push
    animationSpeed: 800,      // 動畫持續時間 (ms)
    timerSpeed: 4000,         // 自動播放間隔 (ms)
    pauseOnHover: true,       // 滑鼠懸停暫停
    startClockOnMouseOut: true,
    startClockOnMouseOutAfter: 1000,
    
    // 導航設定
    navigationArrows: true,   // 顯示左右箭頭
    nextArrowText: '>',
    prevArrowText: '<',
    bulletNavigation: true,   // 圓點導航
    bulletThumbs: false,      // 縮圖導航
    bulletThumbLocation: '',
    
    // 進階設定
    directionalNav: true,     // 方向鍵導航
    captions: true,           // 顯示標題 (img title/alt)
    captionAnimation: 'fade', // slide | fade
    captionAnimationSpeed: 600,
    
    // 回呼函式
    beforeSlideChange: function() {},
    afterSlideChange: function() {},
    afterLoadComplete: function() {}
  });
});

常見問題與解決方案

問題 1:圖片載入前版面跳動 (Layout Shift)


/* CSS 預留空間 */
.orbit-wrapper {
  position: relative;
  width: 100%;
  /* 使用 aspect-ratio 現代寫法 */
  aspect-ratio: 185 / 185; /* 根據實際圖片比例調整 */
  overflow: hidden;
}

/* 或舊版相容寫法 */
.orbit-wrapper::before {
  content: '';
  display: block;
  padding-top: 100%; /* 高度 = 寬度 * 100% */
}

問題 2:jQuery 版本衝突






問題 3:響應式圖片適配


/* CSS 讓圖片自適應容器 */
.orbit img {
  max-width: 100%;
  height: auto;
  display: block;
}

/* 進階:picture 元素 + srcset (現代瀏覽器) */

  
  
  描述

問題 4:無障礙合規


// Orbit 產生的標記需補強:
// 1. 容器加 role="region" aria-label
// 2. 箭頭按鈕加 aria-label="上一張"/"下一張"
// 3. 圓點按鈕加 role="tablist" + aria-label
// 4. 滑鼠/鍵盤焦點時暫停自動播放
// 5. prefers-reduced-motion 媒體查詢停用動畫

@media (prefers-reduced-motion: reduce) {
  .orbit * {
    animation: none !important;
    transition: none !important;
  }
}

現代替代方案推薦

Orbit.js 隨 Foundation 6 移除,不再維護。新專案強烈建議使用:

插件特色體積相依性
Swiper功能最完整、觸控佳、模組化、SSR支援~40KB無
Flickity物理拖拽感、無障礙好、選項豐富~25KB無
Splide輕量、TypeScript、無障礙優先、可擴展~22KB無
Glide.js極輕量、ES模組、無相依~13KB無
Tiny Slider無相依、IE10+、CSS優先~10KB無

Swiper 遷移範例 (推薦)



...
...
import Swiper from 'swiper'; import 'swiper/css'; import 'swiper/css/pagination'; import 'swiper/css/navigation'; new Swiper('.swiper', { loop: true, autoplay: { delay: 4000, disableOnInteraction: false }, pagination: { el: '.swiper-pagination', clickable: true }, navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev' }, effect: 'fade', // 或 'slide', 'cube', 'coverflow', 'flip' fadeEffect: { crossFade: true }, a11y: { enabled: true }, });
copyright © 萬盛學電腦網 all rights reserved