右下角浮出的提示框,常見於系統通知、客服邀請或操作回饋。它的特色是從視窗外滑入、停在角落,可以隨頁面捲動保持位置。以下拆解完整實作。
<style type="text/css">
.messageTip {
border-right: #455690 1px solid; border-top: #a6b4cf 1px solid;
border-left: #a6b4cf 1px solid; border-bottom: #455690 1px solid;
z-index: 99999; left: 0; top: 0;
visibility: hidden; width: 230px;
position: absolute; background: #cfdef4; text-align: left;
}
.messageTip .close { position: absolute; right: 0; font-weight: bold; padding: 4px 4px 0 0; }
.messageTip .close a { color: red; font-size: 12px; text-decoration: none; }
.messageTip .content { border-top: #fff 1px solid; border-left: #fff 1px solid; }
.messageTip .content .title { color: #1f336b; padding: 4px 0 0 4px; height: 22px; }
.messageTip .content .msg {
border-right: #b9c9ef 1px solid; border-left: #728eb8 1px solid;
border-top: #728eb8 1px solid; padding: 10px; margin: 1px;
}
</style>
<div id="eMeng" class="messageTip">
<div class="close"><a href="javascript:msgTip.close();" title="關閉">×</a></div>
<div class="content">
<div class="title">系統提示:</div>
<div class="msg">內容</div>
</div>
</div>
function messageTip(pJso) {
_.init(this, pJso, { name: 'msg' }); // 提示框標籤 ID
this.eMsg = document.getElementById(this.name);
}
messageTip.prototype = {
// 讓提示框貼在視窗右下角
rePosition: function (_this) {
var divHeight = parseInt(_this.eMsg.offsetHeight, 10);
var divWidth = parseInt(_this.eMsg.offsetWidth, 10);
var docWidth = document.body.clientWidth;
var docHeight = document.body.clientHeight;
_this.eMsg.style.top = docHeight - divHeight + parseInt(document.body.scrollTop, 10);
_this.eMsg.style.left = docWidth - divWidth + parseInt(document.body.scrollLeft, 10);
},
// 逐格往上移動
moveDiv: function (_this) {
try {
if (parseInt(_this.eMsg.style.top, 10) <=
(_this.docHeight - _this.divHeight + parseInt(document.body.scrollTop, 10))) {
window.clearInterval(_this.objTimer);
_this.objTimer = window.setInterval(function () { _this.rePosition(_this); }, 1);
}
_this.divTop = parseInt(_this.eMsg.style.top, 10);
_this.eMsg.style.top = _this.divTop - 1;
} catch (e) {}
},
close: function () {
this.eMsg.style.visibility = 'hidden';
if (this.objTimer) window.clearInterval(this.objTimer);
},
show: function () {
this.divTop = parseInt(this.eMsg.style.top, 10);
this.divHeight = parseInt(this.eMsg.offsetHeight, 10);
var divWidth = parseInt(this.eMsg.offsetWidth, 10);
var docWidth = document.body.clientWidth;
this.docHeight = document.body.clientHeight;
this.eMsg.style.top = parseInt(document.body.scrollTop, 10) + this.docHeight + 10;
this.eMsg.style.left = parseInt(document.body.scrollLeft, 10) + docWidth - divWidth;
this.eMsg.style.visibility = "visible";
var _this = this;
this.objTimer = window.setInterval(function () { _this.moveDiv(_this); }, 10);
}
};
var msgTip = new messageTip({ name: 'eMeng' });
window.onload = function () { msgTip.show(); };
window.onresize = function () { msgTip.rePosition(msgTip); };
整個動畫的核心是 setInterval 每 10 毫秒把 top 減 1,讓提示框從視窗底部外一格一格浮進來;到位後切換成另一個計時器持續校正位置,以對抗捲動。
用 position:fixed 就不需要任何計算。程式中大量的 scrollTop、clientHeight、offsetHeight 加減,全是為了模擬 fixed 的效果:
.messageTip {
position: fixed;
right: 0; bottom: 0;
transform: translateY(100%);
transition: transform .3s ease;
}
.messageTip.show { transform: translateY(0); }
這樣連 JS 動畫都不必寫,一行 class 切換即可,resize 時瀏覽器自動處理,效能也更好(transform 走合成層,不觸發重排)。
其他可簡化之處:用 try/catch 包住整段 moveDiv 是在掩蓋錯誤而非處理錯誤,該找出根本原因;_.init 依賴一個未說明的工具物件,可讀性差;href="javascript:..." 應改為 <button>;計時器間隔 10 毫秒對瀏覽器而言過於頻繁,會與其他動畫搶資源。
可訪問性:提示框應加上 role="status" 與 aria-live="polite",讓螢幕閱讀器能播報內容;關閉按鈕要有 aria-label="關閉提示"。若要自動消失,記得給足夠時間並讓使用者能隨時叫回。