完整可執行範例
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>右下角提示框</title>
<style>
#tip { position:fixed; right:0; bottom:0; width:180px; height:0; overflow:hidden;
border:1px solid #ccc; background:#eee; display:none; z-index:10; font-size:12px; }
#tip h1 { height:25px; line-height:25px; background:#0066cc; color:#fff; padding:0 10px; margin:0; }
#tip h1 a { float:right; color:#fff; text-decoration:none; }
#tip p { padding:6px; margin:0; }
#shadow { position:fixed; inset:0; background:#000; opacity:0.7; display:none; z-index:11; }
#detail { position:fixed; left:30%; top:30%; width:500px; height:200px; border:3px double #ccc; background:#fff; display:none; z-index:30; }
</style></head><body>
<div id="shadow"></div>
<div id="detail"></div>
<div id="tip">
<h1><a href="javascript:void(0)" onclick="toggleTip()">×</a>您有新訊息</h1>
<p><a href="javascript:void(0)" onclick="showDetail()">點擊查看詳細</a></p>
</div>
<script>
var timer, handle;
function toggleTip() {
var tip = document.getElementById('tip');
var h = parseInt(tip.style.height) || 0;
if (h === 0) { tip.style.display='block'; animate('up'); }
else animate('down');
}
function animate(dir) {
var tip = document.getElementById('tip');
var h = parseInt(tip.style.height) || 0;
clearInterval(handle);
handle = setInterval(function () {
if (dir === 'up') {
h += 8; if (h >= 200) { h = 200; clearInterval(handle); }
} else {
h -= 8; if (h <= 0) { h = 0; clearInterval(handle); tip.style.display='none'; }
}
tip.style.height = h + 'px';
}, 2);
}
function showDetail() {
document.documentElement.style.overflow = 'hidden';
document.getElementById('shadow').style.display = 'block';
document.getElementById('detail').style.display = 'block';
}
function hideDetail() {
document.documentElement.style.overflow = 'auto';
document.getElementById('shadow').style.display = 'none';
document.getElementById('detail').style.display = 'none';
}
// 自動輪播
window.onload = function () {
(function loop() { toggleTip(); setTimeout(loop, 6000); })();
};
</script></body></html>
關鍵點
- 使用
position:fixed 固定右下角
- 高度動畫
0 → 200px 模擬滑入/滑出
- 遮罩層防止背景互動
setTimeout 迴圈實現定時彈出