Spaces:
Running
Running
Download app.js from u11310019/interactive-multi-web: direct link, hf CLI and curl.
- Browser
- Download file 8.43 kB
-
https://huggingface.co/spaces/u11310019/interactive-multi-web/resolve/main/app.js
- Command line
-
hf download hf://spaces/u11310019/interactive-multi-web/app.js
-
curl -L -o app.js https://huggingface.co/spaces/u11310019/interactive-multi-web/resolve/main/app.js
8.43 kB
| /** | |
| * 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); | |
| }); | |