import { ref, onMounted } from 'vue';export function useCalculateSwiperHeight(headerSelector: string = '.header-search', tabsWrapperSelector: string = '.u-tabs .u-tabs__wrapper') {const swiperHeight = ref<number>(0);const getSystemInfo = () => {return new Promise<UniApp.GetSystemInfoResult>((resolve) => {uni.getSystemInfo({ success: resolve });});};const getRect = (selector: string) => {return new Promise<UniNamespace.NodeInfo | null>((resolve) => {uni.createSelectorQuery().select(selector).boundingClientRect((res) => {const result = Array.isArray(res) ? res[0] : res;resolve(result as UniNamespace.NodeInfo);}).exec();});};const calculateHeight = async () => {try {const [searchRect, tabsRect, sysInfo] = await Promise.all([getRect(headerSelector), getRect(tabsWrapperSelector), getSystemInfo()]);if (!searchRect || !tabsRect || !sysInfo) {console.error('未能获取到必要的布局信息');return;}swiperHeight.value = sysInfo.windowHeight - (searchRect.height || 0) - (tabsRect.height || 0);} catch (error) {console.error('计算高度时发生错误:', error);}};onMounted(() => {calculateHeight();});return {swiperHeight,recalculate: calculateHeight};
}
import { useCalculateSwiperHeight } from '@/hooks/useCalculateSwiperHeight';
const { swiperHeight } = useCalculateSwiperHeight('.header-search', '.u-tabs .u-tabs__wrapper');
const { swiperHeight } = useCalculateSwiperHeight('.header-search', '.u-tabs >>> .u-tabs__wrapper');