功能特性
- 🎯 模態遮罩層 (可配置顏色/透明度)
- 🖱️ 標題欄拖拽移動 (邊界限制)
- 📐 自動居中 + 窗口 resize 跟隨
- 📝 內嵌輸入框表單
- 🔧 回調函數支持 (確認/取消)
- 🌐 IE6+ 兼容 (含 iframe 遮罩 select 穿透)
核心實現 (精簡版)
function AlertWin(options) {
this.config = $.extend({
title: '提示',
message: '',
width: 350,
height: 180,
hasInput: false,
inputPlaceholder: '',
inputValue: '',
confirmText: '確定',
cancelText: '取消',
onConfirm: null, // function(value, closeFn)
onCancel: null, // function(closeFn)
showMask: true,
maskOpacity: 0.3,
draggable: true
}, options);
this._render();
this._bindEvents();
this._center();
}
AlertWin.prototype = {
_render: function() {
var c = this.config;
var html = '';
// 遮罩層
if (c.showMask) {
this.$mask = $('').css({
position: 'fixed', top: 0, left: 0, width: '100%', height: '100%',
background: '#000', opacity: c.maskOpacity, zIndex: 10000,
display: 'none'
}).appendTo('body');
// IE6 select 穿透修復
if ($.browser.msie && parseInt($.browser.version) < 7) {
this.$mask.append('
');
}
}
// 彈窗主體
this.$box = $('
').css({
position: 'fixed', zIndex: 10001, display: 'none',
width: c.width, background: '#fff', border: '1px solid #666',
boxShadow: '0 4px 12px rgba(0,0,0,0.3)'
}).appendTo('body');
// 標題欄
this.$titleBar = $('
').css({
height: '30px', lineHeight: '30px', padding: '0 10px',
background: '#666', color: '#fff', fontWeight: 'bold',
cursor: c.draggable ? 'move' : 'default',
borderBottom: '1px solid #999'
}).text(c.title).appendTo(this.$box);
// 關閉按鈕
$('
×').css({
float: 'right', cursor: 'pointer', fontSize: '18px', lineHeight: '30px'
}).appendTo(this.$titleBar);
// 內容區
this.$content = $('
').css({
padding: '20px', textAlign: 'center'
}).appendTo(this.$box);
if (c.hasInput) {
this.$input = $('
').css({
width: '80%', padding: '8px', fontSize: '14px', border: '1px solid #ccc', borderRadius: '3px'
}).val(c.inputValue).attr('placeholder', c.inputPlaceholder).appendTo(this.$content);
} else {
this.$content.text(c.message);
}
// 底部按鈕
var $btnWrap = $('
copyright ©
萬盛學電腦網 all rights reserved