在 Web 開發中,彈出層(Lightbox/Modal)是常見的交互組件。本文通過 ClassyLightbox 插件,展示如何實現支持多種尺寸、AJAX 內容加載、背景樣式切換、拖拽等功能的彈出對話框。
<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" />
$(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
視窗縮放時是否居中
最佳實踐建議
- 統一在
$(document).ready() 中初始化全域配置
- 使用
destroy() 切換配置前清理舊實例
- AJAX 內容加載建議在
opened 回調中執行,確保容器已就緒
- 按鈕回調中區分
Close 與業務按鈕,避免誤觸發業務邏輯
- 若需拖拽功能,務必引入 jQuery UI
結語
ClassyLightbox 提供了豐富的配置選項與事件回調,能滿足大多數彈出層場景需求。通過合理封裝配置切換邏輯,可進一步簡化業務代碼,提升維護性。
- 上一篇程序編程: jQuery 判斷元素是否存在的最佳實踐
- 下一篇程序編程: JavaScript Function 原型深度解析:從聲明到應用
推薦文章
- JavaScript 判斷變量狀態:try-catch 區分「未聲明」、「已聲明…
- JavaScript 調試利器 console.log 完整指南:語法、格式化、…
- jQuery 獲取元素座標位置完整指南:offset、position 與視口座…
- AngularJS 依賴注入(DI)機制全解析:Value、Factory、Se…
- JavaScript 迴圈中綁定事件:閉包解決參數捕獲問題
- JavaScript 判斷滑鼠是否在 DIV 區域內的三種方法比較</x2Tit…
- 4 個原生 JavaScript 高頻工具函數封裝推薦
- jQuery 操作 iframe 完全指南:同域/跨域訪問、內容查找、高度自適應…
- JavaScript 實作 Sleep 延遲函數:從阻塞到非阻塞的演進
- PHP 雙重保險登錄機制:Session + Cookie 實現「記住我」功能完…