手写一个Vue版本的swiper
时间: 2023-10-15 11:03:52 浏览: 47
好的,以下是一个简单的手写Vue版本的swiper:
```vue
<template>
<div class="swiper">
<div class="swiper-container" ref="swiperContainer">
<div class="swiper-wrapper" ref="swiperWrapper">
<div class="swiper-slide" v-for="(item, index) in swiperList" :key="index">
{{ item }}
</div>
</div>
</div>
<div class="swiper-pagination" ref="pagination"></div>
<div class="swiper-button-prev" ref="prev"></div>
<div class="swiper-button-next" ref="next"></div>
</div>
</template>
<script>
export default {
name: "Swiper",
data() {
return {
swiperList: ["slide1", "slide2", "slide3"],
activeIndex: 0,
timer: null,
};
},
mounted() {
this.init();
},
methods: {
init() {
this.setPagination();
this.setButton();
this.autoPlay();
},
setPagination() {
const pages = this.swiperList.length;
for (let i = 0; i < pages; i++) {
const page = document.createElement("span");
page.className = "swiper-pagination-bullet";
page.addEventListener("click", () => {
this.goToPage(i);
});
this.$refs.pagination.appendChild(page);
}
this.updatePagination();
},
setButton() {
this.$refs.prev.addEventListener("click", () => {
this.goToIndex(this.activeIndex - 1);
});
this.$refs.next.addEventListener("click", () => {
this.goToIndex(this.activeIndex + 1);
});
},
autoPlay() {
this.timer = setInterval(() => {
this.goToIndex(this.activeIndex + 1);
}, 2000);
},
goToPage(index) {
this.activeIndex = index;
this.updatePagination();
this.updateWrapper();
},
goToIndex(index) {
const pages = this.swiperList.length;
if (index < 0) {
index = pages - 1;
} else if (index >= pages) {
index = 0;
}
this.activeIndex = index;
this.updatePagination();
this.updateWrapper();
},
updatePagination() {
const pages = this.$refs.pagination.children;
for (let i = 0; i < pages.length; i++) {
pages[i].classList.remove("swiper-pagination-bullet-active");
}
pages[this.activeIndex].classList.add("swiper-pagination-bullet-active");
},
updateWrapper() {
const width = this.$refs.swiperContainer.clientWidth;
this.$refs.swiperWrapper.style.transform = `translateX(${-width * this.activeIndex}px)`;
},
},
beforeDestroy() {
clearInterval(this.timer);
},
};
</script>
<style scoped>
.swiper {
position: relative;
width: 100%;
overflow: hidden;
}
.swiper-container {
position: relative;
width: 100%;
height: 100%;
white-space: nowrap;
}
.swiper-wrapper {
display: inline-block;
font-size: 0;
transition: transform 0.3s;
}
.swiper-slide {
display: inline-block;
width: 100%;
height: 100%;
font-size: 16px;
text-align: center;
box-sizing: border-box;
padding-top: 50px;
}
.swiper-pagination {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
}
.swiper-pagination-bullet {
display: inline-block;
width: 10px;
height: 10px;
margin: 0 5px;
border-radius: 50%;
background-color: #ccc;
cursor: pointer;
}
.swiper-pagination-bullet-active {
background-color: #333;
}
.swiper-button-prev,
.swiper-button-next {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 20px;
height: 20px;
border-radius: 50%;
background-color: rgba(0, 0, 0, 0.3);
cursor: pointer;
}
.swiper-button-prev {
left: 10px;
}
.swiper-button-next {
right: 10px;
}
</style>
```
这个手写的swiper实现了基本的轮播功能,并且支持自动轮播、分页和左右切换按钮。但是,这个swiper还有很多可以改进的地方,比如添加动画效果、支持响应式布局等。如果需要更加完善的swiper,可以考虑使用第三方库,如swiper、vue-awesome-swiper等。