Files
cz-web/src/views/home/cpns/HomeBanner.vue
2026-10-13 15:12:51 +08:00

296 lines
6.3 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import bannerIllustration from '@/assets/home/banner-illustration.png'
import type { Banner } from '@/types/business/home'
const props = defineProps<{
banners: Banner[]
}>()
const router = useRouter()
const activeIndex = ref(0)
let timer: ReturnType<typeof setInterval> | null = null
// 后端未下发banner 时退回内置插图。插图在设计稿中并非铺满背景,
// 而是定位在右侧 (928,104) 尺寸 723x598,需按原比例显示。
const hasBanners = computed(() => props.banners.length > 0)
const slides = computed(() => {
if (props.banners.length > 0) {
return props.banners.map((item) => item.imageUrl)
}
return [bannerIllustration]
})
function goDemand(): void {
router.push('/demand')
}
function goCases(): void {
router.push('/cases')
}
function goTo(index: number): void {
activeIndex.value = index
}
function startAutoplay(): void {
stopAutoplay()
if (slides.value.length <= 1) return
timer = setInterval(() => {
activeIndex.value = (activeIndex.value + 1) % slides.value.length
}, 5000)
}
function stopAutoplay(): void {
if (timer) {
clearInterval(timer)
timer = null
}
}
watch(slides, () => {
activeIndex.value = 0
startAutoplay()
})
onMounted(startAutoplay)
onBeforeUnmount(stopAutoplay)
</script>
<template>
<section class="home-banner">
<div class="home-banner__stage">
<div class="home-banner__carousel" aria-hidden="true">
<div
v-for="(src, index) in slides"
:key="`${src}-${index}`"
class="home-banner__slide"
:class="{ 'home-banner__slide--active': index === activeIndex }"
>
<img
class="home-banner__image"
:class="{ 'home-banner__image--inline': !hasBanners }"
:src="src"
alt=""
>
</div>
</div>
<div class="home-banner__inner">
<h1 class="home-banner__title">
虫子智造研发一体化平台
</h1>
<p class="home-banner__subtitle">
一站式智能软硬件研发与量产综合服务商
</p>
<div class="home-banner__actions">
<button
class="home-banner__btn home-banner__btn--primary"
type="button"
@click="goDemand"
>
需求提报
</button>
<button
class="home-banner__btn home-banner__btn--ghost"
type="button"
@click="goCases"
>
案例中心
</button>
</div>
</div>
<div
v-if="slides.length > 1"
class="home-banner__dots"
role="tablist"
aria-label="头图轮播"
>
<button
v-for="(_, index) in slides"
:key="index"
class="home-banner__dot"
:class="{ 'home-banner__dot--active': index === activeIndex }"
type="button"
role="tab"
:aria-selected="index === activeIndex"
@click="goTo(index)"
/>
</div>
</div>
</section>
</template>
<style lang="less" scoped>
.home-banner {
width: 100%;
flex: 0 0 auto;
padding: @spacing-lg @home-gutter 0;
box-sizing: border-box;
background: transparent;
@media (max-width: 1600px) {
padding: @spacing-lg @spacing-lg 0;
}
&__stage {
position: relative;
width: 100%;
max-width: @home-content;
height: 420px;
margin: 0 auto;
overflow: hidden;
border-radius: @radius-base;
background: @home-banner-bg;
}
&__carousel {
position: absolute;
inset: 0;
z-index: 0;
}
&__slide {
position: absolute;
inset: 0;
opacity: 0;
transition: opacity 0.6s ease;
&--active {
opacity: 1;
}
}
&__image {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
&--inline {
position: absolute;
top: 24px;
right: 48px;
left: auto;
bottom: auto;
width: 420px;
height: 348px;
margin-left: auto;
object-fit: contain;
}
}
&__inner {
position: relative;
z-index: 1;
width: 100%;
height: 100%;
pointer-events: none;
}
&__title {
position: absolute;
top: 96px;
left: 64px;
margin: 0;
color: @banner-title-color;
font-size: @font-size-hero;
font-weight: 700;
line-height: 1.25;
white-space: nowrap;
}
&__subtitle {
position: absolute;
top: 168px;
left: 64px;
margin: 0;
color: @banner-title-color;
font-size: @font-size-lg;
font-weight: 500;
line-height: 1.4;
white-space: nowrap;
}
&__actions {
position: absolute;
top: 232px;
left: 64px;
.flex(row, flex-start, center);
gap: @spacing-base;
pointer-events: auto;
}
&__btn {
width: 140px;
height: 44px;
border-radius: @radius-base;
font-size: @font-size-base;
font-weight: 500;
line-height: 1;
cursor: pointer;
&--primary {
background: linear-gradient(135deg, @btn-gradient-from 0%, @btn-gradient-to 100%);
border: none;
color: @white;
}
&--ghost {
position: relative;
background: @white;
border: none;
color: @btn-ghost-color;
&::before {
content: '';
position: absolute;
inset: 0;
border-radius: @radius-base;
padding: 2px;
background: linear-gradient(135deg, @btn-gradient-from 0%, @btn-gradient-to 100%);
-webkit-mask:
linear-gradient(#fff 0 0) content-box,
linear-gradient(#fff 0 0);
mask:
linear-gradient(#fff 0 0) content-box,
linear-gradient(#fff 0 0);
-webkit-mask-composite: xor;
mask-composite: exclude;
pointer-events: none;
}
}
}
&__dots {
position: absolute;
left: 50%;
bottom: @spacing-base;
z-index: 1;
.flex(row, center, center);
gap: @spacing-sm;
transform: translateX(-50%);
}
&__dot {
width: 6px;
height: 6px;
padding: 0;
border: none;
border-radius: 50%;
background: @home-banner-dot;
cursor: pointer;
&--active {
width: 28px;
border-radius: 3px;
background: @home-banner-dot;
}
}
}
</style>