頁面右側固定懸浮按鈕,點擊「展開」滑出 QQ 客服與電話諮詢面板,再次點擊「收縮」隱藏,支援平滑動畫。
<div id="floatTools" class="float-panel">
<div class="float-trigger">
<a id="btnExpand" class="btn-open" href="javascript:void(0);" title="展開線上客服">展開</a>
<a id="btnCollapse" class="btn-close" href="javascript:void(0);" title="關閉線上客服" style="display:none;">收縮</a>
</div>
<div id="floatContent" class="float-content" style="display:none;">
<div class="panel-header"></div>
<div class="panel-body">
<ul class="service-group">
<li class="group-title"><h3>QQ 諮詢</h3></li>
<li><span class="label">線上諮詢</span></li>
<li>
<a class="qq-link" target="_blank" href="https://wpa.qq.com/msgrd?v=3&uin=2278808483&site=qq&menu=yes">
<img border="0" src="https://wpa.qq.com/pa?p=2:2278808484:41" alt="客服1" title="客服1">
</a>
</li>
<li>
<a class="qq-link" target="_blank" href="https://wpa.qq.com/msgrd?v=3&uin=954149012&site=qq&menu=yes">
<img border="0" src="https://wpa.qq.com/pa?p=2:954149011:41" alt="客服2" title="客服2">
</a>
</li>
</ul>
<ul class="service-group">
<li class="group-title"><h3>電話諮詢</h3></li>
<li><span class="phone">400-000-000</span></li>
</ul>
<ul class="service-group">
<li class="group-footer">
<h3><a href="https://www.example.com/" target="_blank">官方網站</a></h3>
</li>
</ul>
</div>
</div>
</div>
.float-panel {
position: fixed;
right: 0;
top: 50%;
transform: translateY(-50%);
z-index: 9999;
font-family: "Microsoft YaHei", sans-serif;
}
.float-trigger { width: 36px; }
.btn-open, .btn-close {
display: block;
width: 36px; height: 100px;
background: #d93600;
color: #fff;
text-align: center;
line-height: 100px;
text-decoration: none;
font-size: 14px;
}
.float-content {
position: absolute;
right: 36px; top: 0;
width: 160px;
background: #fff;
border: 1px solid #ddd;
border-right: none;
box-shadow: -2px 0 5px rgba(0,0,0,.1);
}
.panel-header { height: 8px; background: url(header-bg.png) no-repeat; }
.panel-body { padding: 10px 0; }
.service-group { list-style: none; margin: 0; padding: 0 10px; }
.group-title { border-bottom: 1px dashed #eee; padding-bottom: 5px; margin-bottom: 5px; }
.group-title h3 { margin: 0; font-size: 14px; color: #333; }
.label, .phone { display: block; color: #666; font-size: 12px; margin: 5px 0; }
.qq-link img { vertical-align: middle; }
$(function() {
const $panel = $("#floatTools");
const $content = $("#floatContent");
const $btnExpand = $("#btnExpand");
const $btnCollapse = $("#btnCollapse");
// 展開
$btnExpand.on("click", function() {
$content.animate({ width: "show", opacity: "show" }, "normal", function() {
$content.show(); // 確保 display:block
});
$btnExpand.hide();
$btnCollapse.show();
});
// 收縮
$btnCollapse.on("click", function() {
$content.animate({ width: "hide", opacity: "hide" }, "normal", function() {
$content.hide();
});
$btnCollapse.hide();
$btnExpand.show();
});
});
aria-expanded、role="dialog"、鍵盤操作localStorage 記住用戶偏好.float-content {
max-width: 0;
opacity: 0;
overflow: hidden;
transition: max-width 0.3s, opacity 0.3s;
}
.float-panel.expanded .float-content {
max-width: 160px;
opacity: 1;
}
// JS 只需切換 class
document.getElementById("btnExpand").onclick = () =>
document.getElementById("floatTools").classList.add("expanded");
document.getElementById("btnCollapse").onclick = () =>
document.getElementById("floatTools").classList.remove("expanded");