網站側邊常會掛一排客服入口,滑過時展開、移開時收起,頁面向下捲動時還要一路跟著。這份範例把左側與右側兩個浮動區塊都做出來,並支援依客服/業務/備案分組顯示。
頁面由三塊組成:左浮動區、右浮動區、以及一個足夠高的空白佔位區(用來產生捲軸,才能看出浮動效果)。右浮動區內再切出「收起的圖示欄」與「多個可展開的面板」,每個面板有自己的編號。
.qqwid { width:142px; background-image:url(/jscss/demoimg/201011/qq_bg.gif); color:#000000; }
.qqwid a { text-decoration:none; color:#000000; }
.qqwid a:hover { color:#FF0000; }
.qqwid .qqnei { margin-left:5px; margin-right:5px; line-height:25px; }
.qqwid .qqnei img { margin-left:3px; margin-right:5px; vertical-align:middle; }
.qqwid td { font-size:12px; height:25px; line-height:25px; }
三個入口分別對應三個面板,滑入時帶入編號,滑出時收起;另有關閉鈕把整欄藏起來。
<table width="10" border="0" cellspacing="0" cellpadding="0" id="rightmenu">
<tr><td>
<a href="javascript:" onmouseover="showrightmenuover(1);"
onmouseout="showrightmenuout(1);">
<img src="/jscss/demoimg/201011/qq_Atuo.jpg" border="0"></a>
</td></tr>
<tr><td>
<a href="javascript:" onmouseover="showrightmenuover(2);"
onmouseout="showrightmenuout(2);">
<img src="/jscss/demoimg/201011/qq_Adns.jpg" border="0"></a>
</td></tr>
<tr><td>
<a href="javascript:" onmouseover="showrightmenuover(3);"
onmouseout="showrightmenuout(3);">
<img src="/jscss/demoimg/201011/qq_Beian.jpg" border="0"></a>
</td></tr>
<tr><td align="left">
<a href="javascript:" onclick="divclose('rightmenu');">
<img src="/jscss/demoimg/201011/close.jpg" border="0"></a>
</td></tr>
</table>
每個面板用 showrightmenu_N 當 id,預設 display:none,滑入時才顯示。面板內的每一列就是一個客服連結:
<div class="qqwid" id="showrightmenu_1"
style="filter:alpha(opacity=80); z-index:1; display:none;"
onmouseover="showrightmenuover(1);" onmouseout="showrightmenuout(1);">
<div><img src="/jscss/demoimg/201011/qq_top.gif"></div>
<div class="qqnei">
<table border="0" cellspacing="0" cellpadding="0" style="filter:alpha(opacity=86);">
<tr>
<td>
<img src="https://wpa.qq.com/pa?p=1:539680:17" height="17" border="0" align="middle">
<a href="https://wpa.qq.com/msgrd?v=3&uin=539680&site=#&menu=yes"
target="_blank">在線客服</a>
</td>
</tr>
<!-- 其餘列同結構,換掉 uin 即可指向不同客服 -->
</table>
</div>
<div><img src="/jscss/demoimg/201011/qq_bottom.gif" width="142" height="6"></div>
</div>
其餘面板(showrightmenu_2、showrightmenu_3)結構相同,靠 margin-top 往下錯開;實作時把重複的客服列抽出來用迴圈產生,程式碼會短很多。
直接把 position:absolute 的元素釘在固定 top,捲動時它就會滑出視線。這裡的做法是監聽捲動量,每次只補上差距的一小部分,看起來就有平滑跟隨的效果:
var lastScrollY = 0;
var setup = 0.02; // 每次追上的比例,越小越慢
function heartBeat() {
var diffY = document.documentElement.scrollTop;
var percent = setup * (diffY - lastScrollY);
percent = percent > 0 ? Math.ceil(percent) : Math.floor(percent);
document.getElementById("leftfu").style.top =
parseInt(document.getElementById("leftfu").style.top) + percent + "px";
document.getElementById("rightfu").style.top =
parseInt(document.getElementById("rightfu").style.top) + percent + "px";
lastScrollY += percent;
}
window.setInterval("heartBeat()", 1);
function divclose(name) {
document.getElementById(name).style.visibility = 'hidden';
}
function showleftmenuover(id) { document.getElementById("showleftmenu_" + id).style.display = "block"; }
function showleftmenuout(id) { document.getElementById("showleftmenu_" + id).style.display = "none"; }
function showrightmenuover(id){ document.getElementById("showrightmenu_" + id).style.display = "block"; }
function showrightmenuout(id) { document.getElementById("showrightmenu_" + id).style.display = "none"; }
改用 fixed 定位。 現代瀏覽器有 position:fixed,一行 CSS 就能達到浮動效果,不必跑 1 毫秒一次的 setInterval。上面那套 heartBeat 是為了兼顧 IE6 而存在的產物。
滑入滑出容易閃。 面板與圖示欄之間若有空隙,滑鼠經過時面板會收掉。實務上會在外層包一個共同的容器,把 mouseover/mouseout 掛在容器上,或加一點延遲再收。
用 aria 標記狀態。 給收起的圖示加上 aria-expanded 與可讀的 alt,螢幕閱讀器才能理解這是個可展開的控制項。