效果图

子组件
<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>

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
父组件
//普通模式
<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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24