feat: 首页竖屏适配
- 首页Carousel高度根据屏幕宽度变化 - 首页推荐栏目卡片数量随屏幕宽度改变
This commit is contained in:
@ -7,19 +7,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(products.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 products.slice((n - 1) * 3, n * 3)"
|
||||
v-for="(item, index) in pageProducts(n)"
|
||||
:key="index"
|
||||
class="recommend-card"
|
||||
@click="handleProductCardClick(item.id.toString() || '')"
|
||||
@ -64,9 +65,32 @@
|
||||
},
|
||||
});
|
||||
|
||||
const carouselHeight = ref<string>('auto');
|
||||
const perPage = ref(3);
|
||||
const carouselItem = ref<HTMLElement | null>(null);
|
||||
|
||||
const { getImageUrl } = useDirectusImage();
|
||||
const { height } = useElementSize(carouselItem);
|
||||
|
||||
const products = computed(() => props.homepageData?.recommendProducts || []);
|
||||
const pages = computed(() =>
|
||||
Math.ceil(products.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 pageProducts = (n: number) => {
|
||||
return products.value.slice((n - 1) * perPage.value, n * perPage.value);
|
||||
};
|
||||
|
||||
const handleProductCardClick = (documentId: string) => {
|
||||
// 使用路由导航到产品详情页
|
||||
@ -76,6 +100,21 @@
|
||||
router.push(localePath(`/products/${documentId}`));
|
||||
}
|
||||
};
|
||||
|
||||
watch(height, (h) => {
|
||||
if (h > 0) {
|
||||
carouselHeight.value = h + 40 + 'px';
|
||||
}
|
||||
});
|
||||
|
||||
onMounted(() => {
|
||||
updatePerPage();
|
||||
window.addEventListener('resize', updatePerPage);
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
window.removeEventListener('resize', updatePerPage);
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
@ -126,7 +165,7 @@
|
||||
.recommend-list {
|
||||
display: flex;
|
||||
padding: 1rem;
|
||||
height: 400px;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
.recommend-card-group {
|
||||
@ -134,7 +173,7 @@
|
||||
gap: 1rem;
|
||||
width: 100%;
|
||||
margin: 0 auto;
|
||||
height: 100%;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
.recommend-card {
|
||||
@ -173,4 +212,16 @@
|
||||
width: 100%;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
@media (max-width: 1024px) {
|
||||
.recommend-card {
|
||||
width: 50%;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.recommend-card {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user