(targetTab, animate = true)
| 915 | |
| 916 | // 更新滑块位置 |
| 917 | function updateTabSlider(targetTab, animate = true) { |
| 918 | const slider = document.querySelector('.tab-slider'); |
| 919 | const tabs = document.querySelector('.tabs'); |
| 920 | if (!slider || !tabs || !targetTab) return; |
| 921 | |
| 922 | // 获取按钮位置和容器宽度 |
| 923 | const tabLeft = targetTab.offsetLeft; |
| 924 | const tabWidth = targetTab.offsetWidth; |
| 925 | const tabsWidth = tabs.scrollWidth; |
| 926 | |
| 927 | // 使用 left 和 right 同时控制,确保动画同步 |
| 928 | const rightValue = tabsWidth - tabLeft - tabWidth; |
| 929 | |
| 930 | if (animate) { |
| 931 | slider.style.left = `${tabLeft}px`; |
| 932 | slider.style.right = `${rightValue}px`; |
| 933 | } else { |
| 934 | // 首次加载时不使用动画 |
| 935 | slider.style.transition = 'none'; |
| 936 | slider.style.left = `${tabLeft}px`; |
| 937 | slider.style.right = `${rightValue}px`; |
| 938 | // 强制重绘后恢复过渡 |
| 939 | slider.offsetHeight; |
| 940 | slider.style.transition = ''; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | // 初始化滑块位置 |
| 945 | function initTabSlider() { |
no outgoing calls
no test coverage detected