效果流程
- 頁面載入 1 秒後,右下角滑入廣告層(
slideDown)
- 9 秒後自動滑出隱藏(
slideUp),同時顯示「你快回來」小標籤
- 點擊廣告層「關閉」鈕 → 立即隱藏、顯示小標籤
- 滑鼠移入小標籤 → 重新滑入廣告層
完整程式碼
<style>
#keleyislide { width:300px; height:200px; border:1px solid #000; position:fixed; bottom:2px; right:2px; display:none; background:#fff; z-index:9999; }
#keleyislide a { position:absolute; top:8px; right:8px; font-size:12px; color:Blue; text-decoration:none; }
#keleyislide h2 { font-size:24px; text-align:center; font-family:'微軟雅黑'; }
#reshow { position:fixed; right:2px; bottom:2px; font-size:12px; display:none; background:#fff; cursor:pointer; border:1px solid #000; }
</style>
<div id="reshow">你快回來</div>
<div id="keleyislide">
<a href="javascript:void(0)" id="close">關閉</a>
<span style="line-height:50px;">右下角滑動彈出可重現層<br /></span>
</div>
<script src="jquery-1.11.2.min.js"></script>
<script>
$(function() {
function showAd() { $('#keleyislide').slideDown('slow'); $('#reshow').hide(); }
function hideAd() { $('#keleyislide').slideUp('slow'); $('#reshow').show(); }
setTimeout(showAd, 1000);
setTimeout(hideAd, 9000);
$('#close').click(hideAd);
$('#reshow').mouseover(showAd);
});
</script>
優化建議
- Cookie 記憶:使用者關閉後 24 小時不再彈出,
$.cookie('adClosed', '1', {expires:1})。
- 響應式:螢幕寬 < 480px 時改為底部通欄、全寬展示,避免遮擋內容。
- 無障礙:
role="dialog"、aria-labelledby、Esc 鍵關閉、焦點陷阱。
- 效能:
slideDown/Up 觸發重排,改用 transform: translateY() + transition 交給 GPU。
CSS Transform 版核心
#ad { position:fixed; right:20px; bottom:20px; transform:translateY(100%); transition:transform 0.5s ease; }
#ad.show { transform:translateY(0); }
#reshow { position:fixed; right:20px; bottom:20px; opacity:0; pointer-events:none; transition:opacity 0.3s; }
#reshow.show { opacity:1; pointer-events:auto; }
// JS 僅切換 class
const ad = document.getElementById('ad'), tag = document.getElementById('reshow');
function show() { ad.classList.add('show'); tag.classList.remove('show'); }
function hide() { ad.classList.remove('show'); tag.classList.add('show'); }