很多網站會在右下角放一個小懸浮塊,用來承載客服入口、意見反饋或公告。這個示例實現了核心部分:元素固定貼在視窗右下角,鼠標移入展開、點擊關閉則收起,並用跑馬燈承載滾動提示文字。
<style type="text/css">
* { margin:0; padding:0; }
body { text-align:center; }
#main {
border: red 1px solid;
width: 1000px; height: 1600px;
margin: 0 auto;
}
#main #scroll {
width: 250px; height: 150px;
border: green 1px solid;
text-align: left;
position: absolute;
visibility: hidden;
}
#main #scroll #open { float:left; text-align:center; width:180px; }
#main #scroll #close { float:right; }
</style>
注意 #main 給了 1600px 的高度,就是為了製造足夠的滾動空間,方便驗證懸浮定位是否正確。
<div id="main">
僅實現核心部分,至於內容自己完善,或者繼續美化一下,應該不錯!
<div id="scroll">
<div id="open" onmouseover="openbox()">
<a href="/" onclick="openbox()">歡迎光臨</a>
<div id="close" class="zzsky">
<marquee align="left" onmouseover="stop()" onmouseout="start()">
歡迎提出寶貴建議!
</marquee>
</div>
</div>
<div id="close"><a href="#" onclick="closebox()">關閉</a></div>
</div>
</div>
<script type="text/javascript">
var scroll = document.getElementById("scroll");
var main = document.getElementById("main");
var open = document.getElementById("open");
var close = document.getElementById("close");
scroll.style.visibility = "visible";
function runright() {
/* 這裡 -4 主要是為了顯示得比較好,因為設置了邊框 */
scroll.style.top = document.body.scrollTop
+ document.body.clientHeight - scroll.clientHeight - 4 + "px";
scroll.style.left = document.body.scrollLeft
+ document.body.clientWidth - scroll.clientWidth - 4 + "px";
/* 隔一段時間執行這個函數 */
setTimeout("runright()", 30);
}
/* 關閉:高度一直減 */
function closebox() {
scroll.style.height = scroll.offsetHeight - 4 + "px";
if (scroll.offsetHeight > 20) {
setTimeout("closebox()", 5);
} else {
close.style.visibility = "hidden";
}
}
function openbox() {
if (scroll.offsetHeight < 148) {
close.style.visibility = "visible";
scroll.style.height = scroll.offsetHeight + 2 + "px";
setTimeout("openbox()", 5);
}
}
runright();
</script>
1. 用「視窗尺寸減自身尺寸」定位右下角。公式是 scrollTop + clientHeight - offsetHeight。加上 scrollTop 是因為元素用的是 position:absolute,坐標相對於文檔而非視窗;若不加上滾動量,往下一捲懸浮塊就會飛出視線。
2. 用高度遞變模擬展開/收起動畫。收起時每次減 4 像素直到低於 20,展開時每次加 2 直到接近 148。這是最樸素的「定時器驅動屬性變化」動畫法。
3. 用 30 毫秒輪詢維持定位。runright() 透過 setTimeout 不斷重算位置,等效於「跟隨滾動」。
誠實地說,這份代碼有不少地方在今天需要重寫,這裡逐條列出:
· 用輪詢維持定位是極大的浪費。每 30 毫秒觸發一次 setTimeout,無論頁面是否滾動都在寫入樣式,會持續佔用 CPU。正確做法是用 position:fixed 讓瀏覽器原生處理,完全不需要 JS 參與。
· document.body.scrollTop 有多瀏覽器問題。在標準模式下應讀 document.documentElement.scrollTop,跨瀏覽器取值需同時判斷兩者。
· setTimeout("runright()", 30) 傳字符串等價於 eval。應改為 setTimeout(runright, 30) 或 requestAnimationFrame(runright)。
· 收展動畫用固定步長會導致速度不均。減 4 與加 2 的步長不同,展開與收起的耗時也不一致。改用 CSS transition 一行搞定,且速度自然。
· id 重複。示例中 id="close" 被用了兩次(一個是滾動文字容器,一個是關閉按鈕),這違反 HTML 規範,getElementById 只能取到第一個,是潛在 bug。
· marquee 已廢棄。滾動文字請用 CSS animation 平移,或乾脆換成不滾動的靜態文本。
· 缺少可訪問性考慮。關閉按鈕應是真正的 <button> 而非 <a>,並加上 aria-label;純 onmouseover 觸發的展開對鍵盤用戶不可用,應同時綁定 focus 事件。
<style>
#widget{
position: fixed;
right: 0;
bottom: 0;
width: 250px;
background: #fff;
border: 1px solid #090;
overflow: hidden;
/* 收起狀態:只留標題高度 */
max-height: 32px;
transition: max-height .3s ease;
}
#widget.open{ max-height: 200px; }
#widget h3{
margin: 0; padding: 8px 12px;
cursor: pointer; background: #f2f2f2;
display: flex; justify-content: space-between;
}
#widget .body{ padding: 12px; }
</style>
<div id="widget">
<h3>
<span>歡迎光臨</span>
<button aria-label="收起"/關閉">×</button>
</h3>
<div class="body">歡迎提出寶貴建議!</div>
</div>
<script>
const widget = document.getElementById('widget');
const toggle = widget.querySelector('h3');
const closeBtn = widget.querySelector('button');
toggle.addEventListener('click', () => widget.classList.toggle('open'));
toggle.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
widget.classList.toggle('open');
}
});
closeBtn.addEventListener('click', (e) => {
e.stopPropagation();
widget.style.display = 'none';
});
</script>
對比之下可以看到:position:fixed 讓定位邏輯歸零,transition 讓動畫邏輯歸零,事件監聽取代輪詢,代碼量減少了大半而效果更平滑、性能更好。原示例的價值在於把「懸浮定位 + 收展交互」的組成講清楚了,而這套骨架換上現代實現之後,就是可直接落用的版本。