- 更新布局组件(layouts/components/) * 优化设置抽屉组件(SettingDrawer.vue) * 完善头部通知组件(Notices.vue) * 改进用户中心组件(UserCenter.vue) * 优化标签栏组件(TabBar.vue) - 完善工具指令和Hooks * 更新复制指令(directives/copy.ts) * 优化标签滚动Hook(hooks/useTabScroll.ts) 提升用户界面交互体验
67 lines
2.0 KiB
TypeScript
67 lines
2.0 KiB
TypeScript
import type { NScrollbar } from 'naive-ui'
|
|
import { ref, watchEffect } from 'vue'
|
|
import type { Ref } from 'vue'
|
|
import { throttle } from 'radash'
|
|
|
|
export function useTabScroll(currentTabPath: Ref<string>) {
|
|
const scrollbar = ref<InstanceType<typeof NScrollbar>>()
|
|
const safeArea = ref(150)
|
|
|
|
const handleTabSwitch = (distance: number) => {
|
|
scrollbar.value?.scrollTo({
|
|
left: distance,
|
|
behavior: 'smooth',
|
|
})
|
|
}
|
|
|
|
const scrollToCurrentTab = () => {
|
|
nextTick(() => {
|
|
const currentTabElement = document.querySelector(`[data-tab-path="${currentTabPath.value}"]`) as HTMLElement
|
|
const tabBarScrollWrapper = document.querySelector('.tab-bar-scroller-wrapper .n-scrollbar-container')
|
|
const tabBarScrollContent = document.querySelector('.tab-bar-scroller-content')
|
|
|
|
if (currentTabElement && tabBarScrollContent && tabBarScrollWrapper) {
|
|
const tabLeft = currentTabElement.offsetLeft
|
|
const tabBarLeft = tabBarScrollWrapper.scrollLeft
|
|
const wrapperWidth = tabBarScrollWrapper.getBoundingClientRect().width
|
|
const tabWidth = currentTabElement.getBoundingClientRect().width
|
|
const containerPR = Number.parseFloat(window.getComputedStyle(tabBarScrollContent).paddingRight)
|
|
|
|
if (tabLeft + tabWidth + safeArea.value + containerPR > wrapperWidth + tabBarLeft) {
|
|
handleTabSwitch(tabLeft + tabWidth + containerPR - wrapperWidth + safeArea.value)
|
|
}
|
|
else if (tabLeft - safeArea.value < tabBarLeft) {
|
|
handleTabSwitch(tabLeft - safeArea.value)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
const handleScroll = throttle({ interval: 120 }, (step: number) => {
|
|
scrollbar.value?.scrollBy({
|
|
left: step * 400,
|
|
behavior: 'smooth',
|
|
})
|
|
})
|
|
|
|
const onWheel = (e: WheelEvent) => {
|
|
e.preventDefault()
|
|
if (Math.abs(e.deltaY) > Math.abs(e.deltaX)) {
|
|
handleScroll(e.deltaY > 0 ? 1 : -1)
|
|
}
|
|
}
|
|
|
|
watchEffect(() => {
|
|
if (currentTabPath.value) {
|
|
scrollToCurrentTab()
|
|
}
|
|
})
|
|
|
|
return {
|
|
scrollbar,
|
|
onWheel,
|
|
safeArea,
|
|
handleTabSwitch,
|
|
}
|
|
}
|