link.addEventListener('mouseenter', () => { link.querySelector('.hover-line').style.width = '100%'; }); link.addEventListener('mouseleave', () => { link.querySelector('.hover- line').style.width = '0'; });}); 過渡效果 為導覽列的顯示、隱藏、展開、收縮等狀態變化添加平滑的過渡效果 。 // 使用GSAP庫實現更複雜的動畫 document.querySelector('.mega-menu- toggle').addEventListener('click', () => { const megaMenu = document.querySelector('.mega-menu'); if (megaMenu.classList.contains('active')) { gsap.to(megaMenu, { height: 0, opacity: 0, duration: 0.3, onComplete: () => { megaMenu.classList.remove('active'); } }); } else { megaMenu.classList.add('active'); gsap.fromTo(megaMenu, { height: 0, opacity: 0 }, { height: 'auto', opacity: 1, duration: 0.3 } ); }}); 互動指示器 實現跟隨用戶鼠標或選擇移動的動態指示器,增強視覺反饋。 // 創建一個動態移動的指示器const indicator = document.createElement('div');indicator.className = 'nav- indicator';document.querySelector('.navbar').appendC hild(indicator);document.querySelectorAll('.navbar a').forEach(link => { link.addEventListener('mouseenter', () => { const rect = link.getBoundingClientRect(); indicator.style.width = `${rect.width}px`; indicator.style.left = `${rect.left}px`; indicator.style.opacity = '1'; });});document.querySelector('.navbar').addEventList ener('mouseleave', () => { indicator.style.opacity = '0';});