<body onload="setTimeout(function(){ abc.style.display='none'; }, 5000)">
<div id="abc" style="background:yellow;padding:20px;position:absolute;left:92px;top:38px;width:200px;height:169px;z-index:1">
友情提示:本層 5 秒後會自動消失!
</div>
</body>
onload 頁面載入完成後啟動計時器。setTimeout(函式, 5000) 5 秒後執行匿名函式。abc.style.display = 'none' 隱藏元素(IE/舊瀏覽器支援以 ID 當全域變數存取)。<script>
window.addEventListener('load', function() {
var box = document.getElementById('abc');
setTimeout(function() { box.style.display = 'none'; }, 5000);
});
</script>
function autoHideTip(id, ms) {
var el = document.getElementById(id);
if (!el) return;
setTimeout(function() {
el.style.transition = 'opacity 0.3s';
el.style.opacity = '0';
setTimeout(function() { el.style.display = 'none'; }, 300);
}, ms);
}
// 使用
autoHideTip('notice', 3000);
加入淡出動畫、參數化延遲時間,體驗更專業。