Spaces:
Running
Running
File size: 8,428 Bytes
3c778ff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | /**
* Master Application Controller
* Handles page switching, pill animation, theme accent updates, keyboard navigation,
* and full interactive DRAGGABLE functionality for the floating navigation card.
*/
document.addEventListener('DOMContentLoaded', () => {
const tabs = document.querySelectorAll('.tab-btn');
const pill = document.getElementById('pill');
const iframeWrappers = document.querySelectorAll('.iframe-wrapper');
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
const pageIndicator = document.getElementById('pageIndicator');
const floatingControls = document.getElementById('floatingControls');
const totalTabs = tabs.length;
let currentIndex = 0;
// 各主題的專屬 Pill 配色與發光效果
const themeStyles = [
{
// Apple 簡約鈦金
pillBg: 'linear-gradient(135deg, rgba(245, 245, 247, 0.95), rgba(210, 210, 215, 0.95))',
pillShadow: '0 4px 16px rgba(255, 255, 255, 0.35)',
activeTextColor: '#000000',
statusDotColor: '#34c759'
},
{
// Spotify 翠綠音速
pillBg: 'linear-gradient(135deg, rgba(30, 215, 96, 0.95), rgba(21, 160, 70, 0.95))',
pillShadow: '0 4px 16px rgba(30, 215, 96, 0.45)',
activeTextColor: '#000000',
statusDotColor: '#1ed760'
},
{
// Discord 經典靛藍
pillBg: 'linear-gradient(135deg, rgba(88, 101, 242, 0.95), rgba(114, 137, 218, 0.95))',
pillShadow: '0 4px 16px rgba(88, 101, 242, 0.5)',
activeTextColor: '#ffffff',
statusDotColor: '#5865f2'
}
];
/**
* 更新 Pill 指示器位置與寬度
* JS sets pill.style.left/width to the active button's offsetLeft/offsetWidth
*/
function updatePill(activeBtn, themeIndex) {
if (!pill || !activeBtn) return;
// 關鍵需求:動態計算 offsetLeft 與 offsetWidth
pill.style.left = activeBtn.offsetLeft + 'px';
pill.style.width = activeBtn.offsetWidth + 'px';
// 套用主題色彩樣式
const theme = themeStyles[themeIndex] || themeStyles[0];
pill.style.background = theme.pillBg;
pill.style.boxShadow = theme.pillShadow;
// 更新狀態燈顏色
const statusDot = document.querySelector('.status-dot');
if (statusDot) {
statusDot.style.backgroundColor = theme.statusDotColor;
statusDot.style.boxShadow = `0 0 8px ${theme.statusDotColor}`;
}
}
/**
* 切換至指定的頁籤與視圖
*/
function switchTab(index) {
if (index < 0 || index >= totalTabs) return;
currentIndex = index;
// 1. 更新頁籤按鈕狀態
tabs.forEach((tab, i) => {
if (i === currentIndex) {
tab.classList.add('active');
tab.style.color = themeStyles[currentIndex].activeTextColor;
} else {
tab.classList.remove('active');
tab.style.color = '';
}
});
// 2. 移動 Pill 至新按鈕
const activeTab = tabs[currentIndex];
updatePill(activeTab, currentIndex);
// 3. 切換 Iframe 視圖
iframeWrappers.forEach((wrapper, i) => {
if (i === currentIndex) {
wrapper.classList.add('active');
} else {
wrapper.classList.remove('active');
}
});
// 4. 更新頁碼指示
if (pageIndicator) {
pageIndicator.textContent = `${currentIndex + 1} / ${totalTabs}`;
}
}
// 綁定頁籤點擊事件
tabs.forEach((tab) => {
tab.addEventListener('click', (e) => {
e.stopPropagation(); // 防止觸發拖放
const idx = parseInt(tab.getAttribute('data-index'), 10);
switchTab(idx);
});
});
// 綁定最右側左右切換按鈕 (Prev / Next)
if (prevBtn) {
prevBtn.addEventListener('click', (e) => {
e.stopPropagation();
const targetIndex = (currentIndex - 1 + totalTabs) % totalTabs;
switchTab(targetIndex);
});
}
if (nextBtn) {
nextBtn.addEventListener('click', (e) => {
e.stopPropagation();
const targetIndex = (currentIndex + 1) % totalTabs;
switchTab(targetIndex);
});
}
// 支援鍵盤左右箭頭切換
window.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft') {
const targetIndex = (currentIndex - 1 + totalTabs) % totalTabs;
switchTab(targetIndex);
} else if (e.key === 'ArrowRight') {
const targetIndex = (currentIndex + 1) % totalTabs;
switchTab(targetIndex);
}
});
// 視窗縮放時自動重新計算 Pill 位置與邊界防禦
window.addEventListener('resize', () => {
const activeTab = tabs[currentIndex];
updatePill(activeTab, currentIndex);
// 防禦性檢查:若拖動後視窗變小,自動將導覽面板縮至可視區域內
if (floatingControls && floatingControls.style.left) {
const rect = floatingControls.getBoundingClientRect();
const maxLeft = Math.max(10, window.innerWidth - rect.width - 10);
const maxTop = Math.max(10, window.innerHeight - rect.height - 10);
let curLeft = parseFloat(floatingControls.style.left);
let curTop = parseFloat(floatingControls.style.top);
if (curLeft > maxLeft) floatingControls.style.left = maxLeft + 'px';
if (curTop > maxTop) floatingControls.style.top = maxTop + 'px';
}
});
/* ==========================================================================
可拖動導覽面板邏輯 (Draggable Logic)
========================================================================== */
let isDragging = false;
let startX = 0;
let startY = 0;
let initialLeft = 0;
let initialTop = 0;
// 建立動態防護 Overlay 防止拖放時滑鼠事件被 iframe 捕獲
let dragOverlay = document.querySelector('.drag-overlay');
if (!dragOverlay) {
dragOverlay = document.createElement('div');
dragOverlay.className = 'drag-overlay';
document.body.appendChild(dragOverlay);
}
function startDrag(clientX, clientY, target) {
// 若點擊的是按鈕或其內部元素,則不觸發拖動
if (target.closest('button')) return;
isDragging = true;
floatingControls.classList.add('is-dragging');
dragOverlay.classList.add('active');
const rect = floatingControls.getBoundingClientRect();
// 初始化極限精確定位
floatingControls.style.right = 'auto';
floatingControls.style.bottom = 'auto';
floatingControls.style.left = rect.left + 'px';
floatingControls.style.top = rect.top + 'px';
initialLeft = rect.left;
initialTop = rect.top;
startX = clientX;
startY = clientY;
}
function moveDrag(clientX, clientY) {
if (!isDragging) return;
const deltaX = clientX - startX;
const deltaY = clientY - startY;
let newLeft = initialLeft + deltaX;
let newTop = initialTop + deltaY;
// 邊界防護:限制不可越出螢幕
const rect = floatingControls.getBoundingClientRect();
const minX = 10;
const maxX = window.innerWidth - rect.width - 10;
const minY = 10;
const maxY = window.innerHeight - rect.height - 10;
newLeft = Math.max(minX, Math.min(newLeft, maxX));
newTop = Math.max(minY, Math.min(newTop, maxY));
floatingControls.style.left = newLeft + 'px';
floatingControls.style.top = newTop + 'px';
}
function endDrag() {
if (!isDragging) return;
isDragging = false;
floatingControls.classList.remove('is-dragging');
dragOverlay.classList.remove('active');
}
// 滑鼠拖動事件
floatingControls.addEventListener('mousedown', (e) => {
if (e.button !== 0) return; // 僅回應滑鼠左鍵
startDrag(e.clientX, e.clientY, e.target);
});
window.addEventListener('mousemove', (e) => {
if (isDragging) {
e.preventDefault();
moveDrag(e.clientX, e.clientY);
}
});
window.addEventListener('mouseup', () => {
endDrag();
});
// 觸控螢幕 (Touch) 拖動支援
floatingControls.addEventListener('touchstart', (e) => {
if (e.touches.length === 1) {
startDrag(e.touches[0].clientX, e.touches[0].clientY, e.target);
}
}, { passive: true });
window.addEventListener('touchmove', (e) => {
if (isDragging && e.touches.length === 1) {
moveDrag(e.touches[0].clientX, e.touches[0].clientY);
}
}, { passive: true });
window.addEventListener('touchend', () => {
endDrag();
});
// 初始化首次載入位置
setTimeout(() => {
switchTab(0);
}, 50);
});
|