以下是一個自製的 tips 提示效果,已通過 IE6/IE7/IE8/FF3/Chrome10 的相容性測試。它能針對指定元素或主視窗彈出樣式化的提示框,支援自訂文字、定時消失、背景色與寬度。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<body>
<br><br><br><br><br><br><br><br>
<input id='test'>
<br><br><br><br><br><br><br><br>
<a href='#' id='test2'
onmouseover="tips.show('普通鏈接或按鈕提示-灰色-鼠標離開消失-300像素', 'test2', null, '#000000', 300)"
onmouseout="tips.hidden('test2')">普通鏈接或按鈕提示</a>
<script>
// 提示消息類
var tips = {
temp : {},
/***
* 彈出提示
* @param string msg 提示文字內容
* @param string id 要彈出提示的目標物件的 id,若錯誤/null/false/0 則主窗口彈出
* @param int time 定時消失時間毫秒數,若為 null/0/false 則不定時
* @param string color 提示內容的背景顏色,格式為 #000000
* @param int width 提示窗寬度,默認 300
*/
show : function(msg, id, time, color, width)
{
var target = this._get(id);
if(!target) { id = 'window'; }
// 如果彈出過則移除重新彈出
if(this._get(id + '_tips')) { this.remove(id); }
// 設置默認值
msg = msg || 'error';
color = color || '#ea0000';
width = width || 300;
time = time ? parseInt(time) : false;
if(id == 'window') {
var y = document.body.clientHeight / 2 + document.body.scrollTop;
var x = (document.body.clientWidth - width) / 2;
var textAlign = 'center', fontSize = '15', fontWeight = 'bold';
} else {
// 獲取物件坐標信息
for(var y = 0, x = 0; target != null; y += target.offsetTop, x += target.offsetLeft, target = target.offsetParent);
var textAlign = 'left', fontSize = '12', fontWeight = 'normal';
}
// 彈出提示
var tipsDiv = this._create({
display: 'block', position: 'absolute', zIndex: '1001',
width: (width - 2) + 'px', left: (x + 1) + 'px', padding: '5px',
color: '#ffffff', fontSize: fontSize + 'px', backgroundColor: color,
textAlign: textAlign, fontWeight: fontWeight,
filter: 'Alpha(Opacity=50)', opacity: '0.7'
}, { id: id + '_text', innerHTML: msg, onclick: function(){ tips.hidden(id); } });
document.body.appendChild(tipsDiv);
tipsDiv.style.top = (y - tipsDiv.offsetHeight - 12) + 'px';
document.body.appendChild(this._create({
display: 'block', position: 'absolute', zIndex: '1000',
width: (width + 10) + 'px', height: (tipsDiv.offsetHeight - 2) + 'px',
left: x + 'px', top: (y - tipsDiv.offsetHeight - 11) + 'px',
backgroundColor: color, filter: 'Alpha(Opacity=50)', opacity: '0.7'
}, { id: id + '_bg' }));
if(id != 'window') {
var arrow = this._create({
display: 'block', position: 'absolute', overflow: 'hidden',
zIndex: '999', width: '20px', height: '10px',
left: (x + 20) + 'px', top: (y - 13) + 'px'
}, { id: id + '_arrow' });
arrow.appendChild(this._create({
display: 'block', overflow: 'hidden', width: '0px', height: '10px',
borderTop: '10px solid ' + color, borderRight: '10px solid #fff',
borderLeft: '10px solid #fff', filter: 'Alpha(Opacity=70)', opacity: '0.8'
}));
document.body.appendChild(arrow);
}
// 標記已經彈出
this.temp[id] = id;
// 如果定時關閉
if(time) { setTimeout(function(){ tips.hidden(id); }, time) }
return id;
},
/***
* 隱藏提示
* @param string id 要隱藏提示的 id,隱藏主窗口提示 id 為 window,隱藏所有提示 id 為空
*/
hidden : function(id)
{
if(!id) { for(var i in this.temp) { this.hidden(i); } return; }
var t = this._get(id + '_text'), d = this._get(id + '_bg'), a = this._get(id + '_arrow');
if(t) { t.parentNode.removeChild(t); }
if(d) { d.parentNode.removeChild(d); }
if(a) { a.parentNode.removeChild(a); }
},
_create : function(set, attr)
{
var obj = document.createElement('div');
for(var i in set) { obj.style[i] = set[i]; }
for(var i in attr) { obj[i] = attr[i]; }
return obj;
},
_get : function(id)
{
return document.getElementById(id);
}
};
window.onload = function(){
tips.show('主窗口提示-綠色-不定時-300像素', null, null, '#009900', 300);
tips.show('表單報錯提示-紅色-3000毫秒消失-250像素', 'test', 3000, '#ea0000', 250);
}
document.onclick = function(){
tips.hidden();
}
</script>
這個 tips 類別的設計相當完整:show 方法可指定提示文字、目標元素 id、消失時間、背景色與寬度;若 id 為空則在視窗中央彈出,否則會自動計算目標元素的座標,並在下方加上一個小三角形箭頭指向它。而 hidden 方法則負責移除提示框,不傳 id 時會一次清除所有提示。搭配 document.onclick 監聽,點擊頁面任意處即可關閉提示,操作起來相當順手。