<template> <div class="swiper-box"> <transition-group tag="ul" name="carousel"> <li v-for="(item, index) in pic" :key="item.pid" v-show="index === mark"> <!-- <a href="" class="bannera"> --> <img :src="item.url" alt="" /> <!-- </a> --> </li> </transition-group> <div class="bullet"> <span v-for="(item, index) in pic.length" :key="index" :class="{ active: index === mark }" @click="change(index)" ></span> </div> </div></template><script>export default { props: ["pic"], data() { return { mybanner: [ { id: 0, banner: "/images/product_list/product/chuzhou01.jpg" }, { id: 1, banner: "/images/product_list/product/chuzhou02.jpg" }, { id: 2, banner: "/images/product_list/product/huoli01.jpg" } ], mark: 0, len: 3 }; }, created() { // this.autoPlay(); // setInterval(this.autoPlay, 200) // this.play(); // 开启自动轮播 }, methods: { change(i) { this.mark = i; }, autoPlay() { this.mark++; if (this.mark === this.len) { //len在data中定义了,为banner的个数 this.mark = 0; return; } }, play() { setInterval(this.autoPlay, 3000); } }};</script><style lang="scss" scoped>.swiper-box { position: relative; width: 381px; height: 245px; ul { position: relative; top: 0; left: 0; width: 100%; height: 100%; padding: 0px; overflow: hidden; li { position: absolute; width: 100%; img { width: 100%; height: 100%; vertical-align: middle; } } } .bullet { // width: 100%; height: 30px; position: absolute; left: 50%; bottom: 0; transform: translateX(-50%); span { display: inline-block; margin: 10px; width: 10px; height: 10px; background-color: #a4a4a4; border-radius: 50%; &.active { background-color: #fff; } } } /* 注意顺序不能错乱,-active的类名要配置在-enter类名之前 */ // .carousel-enter-active { // // transform: translateX(0); // opacity: 1; // transition: all 1s ease-in-out; // } // .carousel-leave-active { // // transform: translateX(-100%); // opacity: 0; // transition: all 1s ease-in-out; // } // .carousel-enter { // // transform: translateX(100%); // opacity: 1; // } // .carousel-leave { // // transform: translateX(0); // opacity: 0; // } .carousel-enter-active { // transform: translateX(0); animation: bounce-in 1s; } .carousel-leave-active { // transform: translateX(-100%); animation: bounce-in 1s reverse; } @keyframes bounce-in { 0% { // transform: scale(0); opacity: 0; } 50% { // transform: scale(1.5); opacity: 0.5; } 100% { // transform: scale(1); opacity: 1; } } // 另外,li要添加position:absolute这个属性,它相对于carousel绝对定位。}</style>