原始代碼使用 IE 專屬 filter:alpha(opacity=...)、pixelLeft、setTimeout 輪詢動畫、全域變數污染。下方提供現代標準重構版。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>可拖曳動畫導航選單</title>
<style>
* { box-sizing: border-box; }
body { font: 14px/1.5 system-ui, sans-serif; min-height: 100vh; padding: 20px; }
.draggable-menu {
position: absolute;
left: 100px; top: 100px;
width: 200px;
background: #f0f0f0;
border: 1px solid #ccc;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
user-select: none;
z-index: 1000;
}
.menu-header {
display: flex; align-items: center; justify-content: space-between;
padding: 10px 15px;
background: linear-gradient(#4a90d9, #357abd);
color: #fff;
border-radius: 7px 7px 0 0;
cursor: move;
font-weight: 600;
}
.menu-header::after {
content: '⋮⋮'; font-size: 18px; opacity: 0.7;
cursor: grab;
}
.menu-header:active::after { cursor: grabbing; }
.menu-items {
list-style: none; margin: 0; padding: 0;
overflow: hidden;
max-height: 0;
transition: max-height 0.4s ease, opacity 0.3s ease, padding 0.3s ease;
opacity: 0;
}
.draggable-menu:hover .menu-items,
.draggable-menu:focus-within .menu-items {
max-height: 500px; /* 足夠大容納所有項目 */
opacity: 1;
padding: 8px 0;
}
.menu-items li { position: relative; }
.menu-items a {
display: block; padding: 10px 20px;
color: #333; text-decoration: none;
transition: background 0.15s, color 0.15s, padding-left 0.2s;
white-space: nowrap;
}
.menu-items a:hover,
.menu-items a:focus {
background: #e8f0fe; color: #1a73e8;
padding-left: 28px;
}
.menu-items a::before {
content: ''; position: absolute; left: 12px; top: 50%;
width: 6px; height: 6px; border-radius: 50%;
background: #ccc; transform: translateY(-50%);
transition: background 0.15s, left 0.2s;
}
.menu-items a:hover::before,
.menu-items a:focus::before {
background: #1a73e8; left: 16px;
}
</style>
</head>
<body>
<nav class="draggable-menu" id="menu" tabindex="0">
<div class="menu-header">可拖曳的選單</div>
<ul class="menu-items">
<li><a href="#">選單項目一</a></li>
<li><a href="#">選單項目二</a></li>
<li><a href="#">選單項目三</a></li>
<li><a href="#">選單項目四</a></li>
<li><a href="#">選單項目五</a></li>
</ul>
</nav>
<script>
// 拖曳功能模組
function makeDraggable(element, handle = element) {
let isDragging = false, startX, startY, initLeft, initTop;
handle.addEventListener('mousedown', e => {
if (e.target.tagName === 'A' || e.target.tagName === 'BUTTON') return;
isDragging = true;
startX = e.clientX; startY = e.clientY;
initLeft = element.offsetLeft; initTop = element.offsetTop;
document.body.style.cursor = 'grabbing';
document.body.style.userSelect = 'none';
handle.style.cursor = 'grabbing';
});
document.addEventListener('mousemove', e => {
if (!isDragging) return;
const dx = e.clientX - startX;
const dy = e.clientY - startY;
element.style.left = (initLeft + dx) + 'px';
element.style.top = (initTop + dy) + 'px';
});
document.addEventListener('mouseup', () => {
if (isDragging) {
isDragging = false;
document.body.style.cursor = '';
document.body.style.userSelect = '';
handle.style.cursor = 'grab';
}
});
// 觸控支援
handle.addEventListener('touchstart', e => {
if (e.target.tagName === 'A') return;
isDragging = true;
startX = e.touches[0].clientX; startY = e.touches[0].clientY;
initLeft = element.offsetLeft; initTop = element.offsetTop;
}, { passive: true });
document.addEventListener('touchmove', e => {
if (!isDragging) return;
const dx = e.touches[0].clientX - startX;
const dy = e.touches[0].clientY - startY;
element.style.left = (initLeft + dx) + 'px';
element.style.top = (initTop + dy) + 'px';
}, { passive: false });
document.addEventListener('touchend', () => { isDragging = false; });
}
// 初始化
const menu = document.getElementById('menu');
const header = menu.querySelector('.menu-header');
makeDraggable(menu, header);
// 可選:雙擊標題列重置位置
header.addEventListener('dblclick', () => {
menu.style.left = '100px';
menu.style.top = '100px';
});
</script>
</body>
</html>
| 特性 | 原版 (IE Filter) | 現代重構 |
|---|---|---|
| 動畫驅動 | setTimeout 輪詢修改 pixelLeft/opacity | CSS Transition (GPU 加速) |
| 透明度 | filter:alpha(opacity=0-100) | opacity: 0-1 (標準) |
| 位移 | style.pixelLeft (IE 專屬) | transform: translateX() 或 left/top |
| 拖曳 | 全域變數 + document 事件 | 模組化函數 + 觸控支援 |
| 無障礙 | 無 | :focus-within、tabindex、語意標籤 |
| 響應式 | 固定像素 | 相對單位、max-height 動畫 |
function clampPosition(element) {
const rect = element.getBoundingClientRect();
const maxX = window.innerWidth - rect.width;
const maxY = window.innerHeight - rect.height;
element.style.left = Math.max(0, Math.min(maxX, element.offsetLeft)) + 'px';
element.style.top = Math.max(0, Math.min(maxY, element.offsetTop)) + 'px';
}
// 在 mouseup/touchend 時呼叫
document.addEventListener('mouseup', () => clampPosition(menu));
document.addEventListener('touchend', () => clampPosition(menu));
// 儲存位置
function savePosition() {
localStorage.setItem('menuPos', JSON.stringify({
left: menu.offsetLeft, top: menu.offsetTop
}));
}
// 載入位置
function loadPosition() {
const saved = localStorage.getItem('menuPos');
if (saved) {
const { left, top } = JSON.parse(saved);
menu.style.left = left + 'px';
menu.style.top = top + 'px';
}
}
// 拖曳結束時儲存
document.addEventListener('mouseup', savePosition);
document.addEventListener('touchend', savePosition);
loadPosition();
let closeOnOutsideClick = true; // 可配置
document.addEventListener('click', e => {
if (closeOnOutsideClick && !menu.contains(e.target)) {
menu.classList.remove('open'); // 需配合 .open 類控制顯示
}
});
// 選單內點擊不關閉
menu.addEventListener('click', e => e.stopPropagation());