效果图
子组件
<template>
<div
class="card_box"
ref="cardbox"
@mouseenter="swiperStop()"
@mouseleave="swiper()"
>
<div
class="card_item"
:class="index == boxindex ? 'card_active' : 'card_actives'"
v-for="(item, index) in list"
:key="index"
:style="{ backgroundImage: 'url(' + item + ')' }"
@click="cardClick(index)"
>
<div>{{ index + 1 }}</div>
</div>
</div>
</template>
<script>
export default {
name: "cardbox",
props: {
//数据
list: {
type: Array,
default: () => []
},
//自动播放
bool: {
type: Boolean,
default: false
}
},
data() {
return {
boxindex: 0,
timer: null
};
},
mounted() {
this.swiper();
this.$refs.cardbox.style.width = 300 + (this.list.length - 1) * 105 + "px";
},
methods: {
//卡片点击事件
cardClick(index) {
this.boxindex = index;
},
//自动轮播
swiper() {
if (this.bool == true) {
this.timer = setInterval(() => {
this.boxindex++;
if (this.boxindex > this.list.length - 1) {
this.boxindex = 0;
}
}, 2000);
}
},
//停止轮播
swiperStop() {
if (this.bool == true) {
clearInterval(this.timer);
}
}
},
//去除轮播自动播放
beforeDestroy() {
this.swiperStop();
}
};
</script>
<style lang="scss" scoped>
.card_box {
display: flex;
align-items: center;
// overflow: hidden;
.card_item {
// width: 90px;
height: 300px;
margin-right: 15px;
border-radius: 10px;
box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.3);
background-size: cover;
background-position: center;
background-repeat: no-repeat;
cursor: pointer;
color: #fff;
font-weight: 700;
position: relative;
div {
position: absolute;
bottom: 10px;
width: 100%;
text-align: center;
}
}
.card_active {
animation: card 0.5s cubic-bezier(0.39, 0.575, 0.565, 1) both;
}
@keyframes card {
0% {
width: 90px;
}
100% {
width: 300px;
border-radius: 15px;
}
}
.card_actives {
animation: cards 0.5s cubic-bezier(0.39, 0.575, 0.565, 1) both;
}
@keyframes cards {
0% {
width: 300px;
}
100% {
width: 90px;
border-radius: 10px;
}
}
}
</style>
父组件
//普通模式
<card :list="listimg" ></card>
//自动播放
<card :list="listimg" bool></card>
<script>
export default {
data() {
return {
//图片数据
listimg: [
"https://s3.bmp.ovh/imgs/2022/07/25/3825abda2959a12c.jpg",
"https://s3.bmp.ovh/imgs/2022/07/25/84dfb8476150d96f.jpg",
"https://s3.bmp.ovh/imgs/2022/07/25/00c16a1b9f2dd206.jpg",
"https://s3.bmp.ovh/imgs/2022/07/25/19c0f6bcff1017fc.jpg",
"https://s3.bmp.ovh/imgs/2022/07/25/004af39d0666d17b.jpg"
]
};
},
components: {
//注册挂载之组件
card: () => import("./components/card.vue"),
}
}
</script>
本文含有隐藏内容,请 开通VIP 后查看