feat: 首页竖屏适配

- 首页Carousel高度根据屏幕宽度变化
- 首页推荐栏目卡片数量随屏幕宽度改变
This commit is contained in:
2025-11-01 15:32:15 +08:00
parent 06c30a7ea3
commit d076088747
3 changed files with 119 additions and 10 deletions

View File

@ -5,19 +5,20 @@
<div v-if="!pending">
<el-carousel
class="recommend-carousel"
height="auto"
:height="carouselHeight"
arrow="never"
indicator-position="outside"
:autoplay="false"
>
<el-carousel-item
v-for="n in Math.floor(solutions.length / 3) + 1"
v-for="n in pages"
ref="carouselItem"
:key="n"
class="recommend-list"
>
<div class="recommend-card-group">
<el-card
v-for="(item, index) in solutions.slice((n - 1) * 3, n * 3)"
v-for="(item, index) in pageSolutions(n)"
:key="index"
class="recommend-card"
@click="handleSolutionCardClick(item.id.toString() || '')"
@ -62,11 +63,34 @@
},
});
const carouselHeight = ref<string>('auto');
const perPage = ref(3);
const carouselItem = ref<HTMLElement | null>(null);
const { getImageUrl } = useDirectusImage();
const { height } = useElementSize(carouselItem);
const solutions = computed(
() => props.homepageData?.recommendSolutions || []
);
const pages = computed(() =>
Math.ceil(solutions.value.length / perPage.value)
);
const updatePerPage = () => {
const width = window.innerWidth;
if (width < 768) {
perPage.value = 1;
} else if (width < 1024) {
perPage.value = 2;
} else {
perPage.value = 3;
}
};
const pageSolutions = (n: number) => {
return solutions.value.slice((n - 1) * perPage.value, n * perPage.value);
};
const handleSolutionCardClick = (documentId: string) => {
// 使用路由导航到产品详情页
@ -76,6 +100,22 @@
router.push(localePath(`/solutions/${documentId}`));
}
};
watch(height, (h) => {
if (h > 0) {
carouselHeight.value = h + 40 + 'px';
console.log('carouselHeight updated:', carouselHeight.value);
}
});
onMounted(() => {
updatePerPage();
window.addEventListener('resize', updatePerPage);
});
onBeforeUnmount(() => {
window.removeEventListener('resize', updatePerPage);
});
</script>
<style scoped>
@ -126,7 +166,7 @@
.recommend-list {
display: flex;
padding: 1rem;
height: 400px;
height: auto;
}
.recommend-card-group {
@ -134,7 +174,7 @@
gap: 1rem;
width: 100%;
margin: 0 auto;
height: 100%;
height: auto;
}
.recommend-card {
@ -173,4 +213,16 @@
width: 100%;
border-radius: 4px;
}
@media (max-width: 1024px) {
.recommend-card {
width: 50%;
}
}
@media (max-width: 768px) {
.recommend-card {
width: 100%;
}
}
</style>