萬盛學電腦網

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

jQuery 彈出層與對話框實現指南:ClassyLightbox 完整示例

在 Web 開發中,彈出層(Lightbox/Modal)是常見的交互組件。本文通過 ClassyLightbox 插件,展示如何實現支持多種尺寸、AJAX 內容加載、背景樣式切換、拖拽等功能的彈出對話框。

核心功能特性

  • 靈活的尺寸控制(固定寬度、自適應)
  • 支持 AJAX 動態加載內容
  • 多種背景模式:白色淡入、黑色淡入、無背景、隨機背景
  • 視窗縮放時自動居中
  • 按鈕回調與事件處理
  • 若載入 jQuery UI,自動啟用拖拽功能

HTML 結構示例

<input id="alert" type="button" value="alert('Hello!')" />
<input id="ajax" type="button" value="Ajax Content" />
<input id="buttons" type="button" value="Buttons" />
<input id="small" type="button" value="Small" />
<input id="draggable" type="button" value="Draggable" />
<input id="wbgf" type="button" value="White Fading Background" />
<input id="bbgf" type="button" value="Black Fading Background" />
<input id="bbg" type="button" value="Black (or white!) Background" />
<input id="nobg" type="button" value="No Background" />
<input id="center" type="button" value="Centered on Resize" />

JavaScript 初始化與事件綁定

$(document).ready(function() {
    // 全域初始化配置
    ClassyLightbox.init({
        override: true,
        background: 'white',
        centerOnResize: true,
        fade: true
    });

    // 簡單提示框
    $('#alert').click(function() {
        alert('Hello');
    });

    // AJAX 內容加載
    $('#ajax').click(function() {
        ClassyLightbox.alert({
            width: '400px',
            title: 'Ajax Content',
            rightButtons: ['OK'],
            leftButtons: ['Close'],
            opened: function() {
                $('
').load('demo.html').appendTo('#lbContent'); }, onClick: function(button) { console.log(button); } }); }); // 多按鈕回調 $('#buttons').click(function() { ClassyLightbox.alert({ width: '400px', title: 'Button Callbacks', rightButtons: ['No', 'Yes'], leftButtons: ['Close'], opened: function() { $('
').html("rightButtons: ['No', 'Yes'],
leftButtons: ['Close']").appendTo('#lbContent'); }, onClick: function(button) { if (button !== 'Close') { $('#ClassyLightbox #lbContent').append('
Clicked ' + button); } console.log(button); } }); }); // 小尺寸視窗 $('#small').click(function() { ClassyLightbox.alert({ width: '200px', title: '200px', rightButtons: ['OK'], opened: function() { $('
').html("I am Small.
width: '200px'").appendTo('#lbContent'); }, onClick: function(button) { console.log(button); } }); }); // 可拖拽視窗(需 jQuery UI) $('#draggable').click(function() { ClassyLightbox.alert({ width: '400px', title: 'Drag Me Up Here!', rightButtons: ['Cool!'], opened: function() { $('
').html("If jQuery UI is loaded, all windows will become draggable automagically!").appendTo('#lbContent'); }, onClick: function(button) { console.log(button); if (button === 'Cool!') { ClassyLightbox.clear(); } } }); }); // 背景樣式切換系列 $('#wbgf').click(function() { switchBackground('white', true, true, "Hello World"); }); $('#bbgf').click(function() { switchBackground('black', true, true, "Hello World"); }); $('#bbg').click(function() { const bg = (Math.ceil(Math.random() * 2) === 2) ? 'white' : 'black'; switchBackground(bg, false, false, "My background doesn't have to fade in, it can be distracting at times."); }); $('#nobg').click(function() { switchBackground('none', true, false, "I have no background"); }); $('#center').click(function() { switchBackground('white', true, false, "Resize the window when I am open! I stay centered on resize."); }); // 封裝背景切換邏輯 function switchBackground(bg, fade, center, msg) { ClassyLightbox.destroy(); ClassyLightbox.init({ override: true, background: bg, centerOnResize: center, fade: fade }); alert(msg); } });

關鍵配置選項說明

選項 類型 預設值 說明
width String '400px' 彈出層寬度
title String '' 標題文字
rightButtons Array [] 右側按鈕組
leftButtons Array [] 左側按鈕組
opened Function null 打開後回調,用於注入內容
onClick Function null 按鈕點擊回調,參數為按鈕文本
background String 'white' 背景類型:white/black/none
fade Boolean true 是否淡入動畫
centerOnResize Boolean false 視窗縮放時是否居中

最佳實踐建議

  1. 統一在 $(document).ready() 中初始化全域配置
  2. 使用 destroy() 切換配置前清理舊實例
  3. AJAX 內容加載建議在 opened 回調中執行,確保容器已就緒
  4. 按鈕回調中區分 Close 與業務按鈕,避免誤觸發業務邏輯
  5. 若需拖拽功能,務必引入 jQuery UI

結語

ClassyLightbox 提供了豐富的配置選項與事件回調,能滿足大多數彈出層場景需求。通過合理封裝配置切換邏輯,可進一步簡化業務代碼,提升維護性。

copyright © 萬盛學電腦網 all rights reserved