頁面載入時自動滑下顯示通知條,用戶點擊「我知道了」或關閉按鈕滑上收起,並可選向服務端記錄「已讀」狀態。本文基於 jQuery slideDown/slideUp 實現完整交互流程。
頁面打開 → 通知條從頂部滑下 (2秒) → 用戶點擊關閉 → 通知條滑上收起 → AJAX 通知後端標記已讀。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>通知欄自動展開收縮</title>
<style>
#warn { display: none; } /* 初始隱藏 */
#screefull {
display: block; border: 1px solid #bddbf3; width: 954px; padding: 3px 5px 7px;
margin: 0 auto 10px; background: #fef9db; position: relative;
}
#screefull a.close {
display: block; width: 25px; height: 20px; background: url(img/cx.gif) no-repeat 0 0;
overflow: hidden; float: right; margin: 16px 8px 0 0; text-decoration: none;
}
#screefull p { display: block; text-align: center; font-family: "Microsoft Yahei"; font-size: 14px; color: #000; height: 28px; line-height: 28px; }
#screefull var { display: inline-block; background: url(img/wicon.gif) no-repeat 0 0; width: 28px; height: 28px; vertical-align: middle; margin-right: 10px; }
#screefull em { color: #ff0000; font-family: "Microsoft Yahei"; font-style: normal; }
#screefull span { display: inline-block; vertical-align: middle; }
#screefull p a { color: #2400ff; text-decoration: underline; }
</style>
<script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<div id="warn" class="scree clearfix">
<div id="screefull">
<a href="#" class="close" onclick="closewarn('close')">[x]</a>
<p><span> 在<em>未通過審核的網站</em>添加廣告代碼,將不會產生傭金。<a href="#" target="_blank">我知道了</a></span></p>
</div>
</div>
<script type="text/javascript">
function closewarn(type) {
$("#warn").slideUp("slow"); // 向上滑動隱藏
if (type === 'know') {
// AJAX 通知後端標記已讀
$.post(window.location.href, {op: 'setwarn'}, function(data) {
// 可根據 data 進一步處理
});
}
}
window.onload = function() {
$("#warn").slideDown(2000); // 2秒滑下展開
};
</script>
</body>
</html>
#warn { display: none; } 確保頁面渲染時不佔位、不閃爍。op=setwarn 發送 POST,後端可更新用戶通知狀態,避免下次再彈。$(function(){...}) (DOMContentLoaded) 更早觸發。CSS transition: max-height 0.3s ease + classList.toggle 替代 jQuery 動畫,無依賴、性能更好、可被瀏覽器優化。IntersectionObserver 或 pagehide 確保通知曝光上報。NotificationBar 組件:Props 接收 message, onClose, onDismiss, autoShowDelay,支援多實例、堆疊管理。localStorage 記錄「不再提示」狀態,減少服務端請求。此模式廣泛應用於「系統公告」、「新功能引導」、「Cookie 同意條」等場景,掌握其動畫時序與狀態持久化邏輯極具實戰價值。