頁面左側固定一個標籤,鼠標懸停/點擊時,面板平滑滑出顯示內容;鼠標離開/點擊關閉按鈕時滑回。支持 IE6+(含 position:fixed 模擬)。
<div class="reading" id="sideTip">
<a class="read_trigger" href="javascript:void(0)">
<img src="images/tab_icon.gif" alt="提示" />
</a>
<div class="read">
<a class="read_close" href="javascript:void(0)">
<img src="images/icon_close.gif" alt="關閉" />
</a>
<p>您可以訂閱生意街商機話題,郵箱接收最新內容。</p>
<p class="read_btn">
<a href="#" target="_blank"><img src="images/read02.gif" alt="立即訂閱" /></a>
</p>
</div>
</div>
/* 觸發標籤:始終固定左側 */
.reading {
position: fixed;
left: 0;
bottom: 30px;
width: 25px;
height: 75px;
cursor: pointer;
z-index: 1000;
/* IE6 模擬 fixed */
_position: absolute;
_top: expression(eval(document.documentElement.scrollTop + document.documentElement.clientHeight - this.offsetHeight - 30));
}
/* 滑出面板:初始隱藏在左側外面 */
.read {
position: fixed;
left: -287px; /* 寬度 285px + 邊框 2px */
bottom: 30px;
width: 285px;
height: 100px;
background: #fff;
border: 1px solid #d0d0d0;
box-shadow: 0 1px 2px #ccc;
z-index: 1001;
display: none; /* JS 控制顯示 */
/* IE6 模擬 fixed */
_position: absolute;
_top: expression(eval(document.documentElement.scrollTop + document.documentElement.clientHeight - this.offsetHeight - 30));
}
.read p { font-size: 14px; line-height: 22px; padding: 15px 0 0 16px; width: 240px; }
.read .read_btn { text-align: right; padding-top: 5px; }
.read_close { float: right; padding: 2px; cursor: pointer; }
$(function() {
const $tip = $('#sideTip');
const $panel = $tip.find('.read');
const $trigger = $tip.find('.read_trigger');
const $close = $tip.find('.read_close');
let animating = false;
// 顯示面板
function showPanel() {
if (animating || $panel.is(':visible')) return;
animating = true;
$panel.show().animate({ left: 0 }, 300, 'swing', () => animating = false);
}
// 隱藏面板
function hidePanel() {
if (animating || !$panel.is(':visible')) return;
animating = true;
$panel.animate({ left: -287 }, 300, 'swing', () => {
$panel.hide();
animating = false;
});
}
// 事件綁定
$trigger.on('click', showPanel); // 點擊標籤展開
$close.on('click', hidePanel); // 關閉按鈕
// 可選:鼠標懸停展開/收起
// $tip.hover(showPanel, hidePanel);
// 可選:點擊面板外部關閉
$(document).on('click', e => {
if (!$tip.is(e.target) && $tip.has(e.target).length === 0) {
hidePanel();
}
});
// 可選:ESC 關閉
$(document).on('keydown', e => e.key === 'Escape' && hidePanel());
});
| 技術點 | 實現 | 說明 |
|---|---|---|
| 固定定位 | position:fixed | 相對視口定位,不隨滾動 |
| IE6 模擬 fixed | _position:absolute; _top:expression(...) | 利用 IE 專有 expression 動態計算 top,配合 _hack 隔離 |
| 滑動動畫 | .animate({left:0}) | 從 left:-287px → 0,平滑過渡 |
| 狀態鎖 | animating 變量 | 防止快速重複點擊導致動畫隊列堆疊 |
| 層級管理 | z-index: 1000/1001 | 面板覆蓋在標籤上方 |
/* CSS 動畫版:無 JS 動畫計算,性能更好 */
.read {
/* ... 原有樣式 ... */
left: -287px;
transition: left 0.3s ease-out; /* 核心:CSS 過渡 */
visibility: hidden; /* 隱藏時不可點擊 */
opacity: 0;
}
.read.show {
left: 0;
visibility: visible;
opacity: 1;
}
/* JS 只切換類名 */
const $tip = document.getElementById('sideTip');
const $panel = $tip.querySelector('.read');
const $trigger = $tip.querySelector('.read_trigger');
const $close = $tip.querySelector('.read_close');
$trigger.addEventListener('click', () => $panel.classList.add('show'));
$close.addEventListener('click', () => $panel.classList.remove('show'));
// 點擊外部關閉
document.addEventListener('click', e => {
if (!$tip.contains(e.target)) $panel.classList.remove('show');
});
// ESC 關閉
document.addEventListener('keydown', e => e.key === 'Escape' && $panel.classList.remove('show'));
側邊滑入提示的核心是 fixed 定位 + 負 left 隱藏 + 正 left 顯示 + 動畫過渡。jQuery 版用 .animate() 兼容老瀏覽器;現代版用 CSS transition 更流暢、無阻塞。IE6 兼容通過 expression 模擬 fixed,但現代項目已無需支持 IE6。組件化封裝後,可復用於側邊欄、客服浮窗、優惠券領取等場景。