在頁面上放一條永遠貼齊瀏覽器底部的工具列,用來顯示公告、客服聯絡方式或購物車入口,是很實用的版面元素。加上關閉按鈕之後,使用者看過就不需要一直被它佔用空間。本文示範的版本還搭配了 cookie 記錄關閉狀態與單行滾動公告。
1. 工具列以 position: fixed 固定在視窗底部,捲動頁面時不會跑掉;
2. 關閉後在右側留下一個「開啟」小把手,隨時可以叫回來;
3. 關閉狀態寫入 cookie 並設定一天有效,重新整理後仍維持;
4. 公告區塊每 3 秒向上捲動一行,循環播放。
<!DOCTYPE html>
<html>
<head>
<title>固定底(頂)部菜單</title>
<script type="text/javascript" src="js/jquery1.3.2.js"></script>
<style>
body, h1, h2, h3, h4, h5, h6, p, ul, li, dl, dt, dd { padding: 0; margin: 0; }
li { list-style: none; }
img { border: none; }
em { font-style: normal; }
a { color: #555; text-decoration: none; outline: none; }
a:hover { color: #000; text-decoration: underline; }
body { font-size: 12px; font-family: Arial, Verdana, Helvetica, sans-serif;
word-break: break-all; word-wrap: break-word; }
.bnav {
text-align: left; height: 25px; overflow: hidden; width: 98%; line-height: 25px;
background: #fff; margin: 0 1%; border: #B4B4B4 1px solid; border-bottom: none;
z-index: 999; position: fixed; bottom: 0; left: 0;
_position: absolute; /* for IE6 */
_top: expression(documentElement.scrollTop + documentElement.clientHeight - this.offsetHeight);
}
.close { position: absolute; right: 5px; height: 25px; width: 16px; text-indent: -9999px; padding-left: 10px; }
.close a { background: url(../images/201008/close.gif) no-repeat center; width: 16px; display: block; }
.bnav2 { height: 24px; line-height: 24px; margin: 1px 1px 0; background: #E5E5E5; }
.bnav .s1 { position: absolute; left: 10px; }
.bnav .s1 img { padding-top: 3px; margin-right: 7px; }
.bnav .s2 { position: absolute; right: 30px; color: #888; }
.bnav .s2 span { padding-right: 10px; }
.bnav .s2 a { margin: 0 6px; }
.rolltext { position: absolute; left: 160px; height: 25px; line-height: 25px; overflow: hidden; }
.rolltext dt, .rolltext dd { float: left; width: auto; }
.rolltext a { display: block; height: 25px; overflow: hidden; }
.bnav3 {
height: 25px; width: 16px; line-height: 25px; margin: 0 1%; padding-right: 6px;
border-bottom: none; z-index: 999; position: fixed; bottom: 0; right: 0;
_position: absolute; /* for IE6 */
_top: expression(documentElement.scrollTop + documentElement.clientHeight - this.offsetHeight);
}
.bnav3 a { background: url(../images/201008/open.gif) no-repeat center; display: block;
height: 25px; width: 16px; text-indent: -5000px; }
</style>
</head>
<body>
<script type="text/javascript">
// 讀取關閉狀態
var closeBN = $.cookie("bnav");
if (closeBN == "0") { closeNav(); }
function showNav() {
$(".openClose").toggle();
$.cookie("bnav", "1", { expires: 1 });
}
function closeNav() {
$(".openClose").toggle();
$.cookie("bnav", "0", { expires: 1 });
}
// 單行向上滾動
function SingleScroll() {
$(".rolltext dd").animate({ marginTop: "-25px" }, 500, function () {
$(this).css({ marginTop: "0px" }).find("a:first").appendTo(this);
});
}
$(document).ready(function () {
setInterval(SingleScroll, 3000);
});
</script>
<div class="bnav openClose">
<div class="bnav2">
<span class="s1">
<a href="#"><img src="images/201008/qqonline.gif" alt="線上客服"></a>
<a href="#"><img src="images/201008/qqonline2.gif" alt="線上客服"></a>
</span>
<dl class="rolltext">
<dt>特別聲明:</dt>
<dd>
<a href="#">連假期間商城有專人值班,請放心選購</a>
<a href="#">本店所售手機為大陸行貨、全國聯保商品</a>
<a href="#">部分港行、歐行機型我們都已特別註明</a>
<a href="#">請不要再重複詢問同類問題,客服電話已滿線</a>
</dd>
</dl>
<span class="s2">
<span><a href="#">[登入]</a><a href="#">[免費註冊]</a></span>
<a href="#">購物車</a>|<a href="#">幫助中心</a>|<a href="#">線上留言</a>
</span>
<span class="close">
<a href="javascript:void(0)" onclick="closeNav()" title="關閉">關閉</a>
</span>
</div>
</div>
<div class="bnav3 openClose" style="display:none;">
<a href="javascript:void(0)" onclick="showNav()" title="打開">打開</a>
</div>
</body>
</html>
這段程式碼有幾處值得注意。首先是 IE6 用的 expression() 與 _position hack,在現代專案中可以直接刪除,只留 position: fixed。其次,$.cookie 並非 jQuery 核心方法,需要另外引入 jquery.cookie 外掛;若不想多載一個檔案,用原生 document.cookie 讀寫即可,或者改用 localStorage 更省事。
另外,固定在底部的工具列會遮住頁面最後一段內容,記得幫 body 加上等同工具列高度的 padding-bottom。滾動公告則建議加上 pause 控制,並在 prefers-reduced-motion 生效時停止自動捲動,避免影響有動態敏感困擾的使用者。
希望本文所述對大家的 JavaScript 程式設計有所幫助。