vue实现图片滑动
Vue 实现图片滑动功能
使用第三方库(如 Swiper)
安装 Swiper 库:
npm install swiper
在 Vue 组件中引入 Swiper:
<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="(image, index) in images" :key="index">
<img :src="image" alt="Slide">
</div>
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
</template>
<script>
import Swiper from 'swiper';
import 'swiper/swiper-bundle.css';
export default {
data() {
return {
images: [
'image1.jpg',
'image2.jpg',
'image3.jpg'
]
};
},
mounted() {
new Swiper('.swiper-container', {
loop: true,
pagination: {
el: '.swiper-pagination',
clickable: true,
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
}
};
</script>
<style scoped>
.swiper-container {
width: 100%;
height: 300px;
}
.swiper-slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
手动实现图片滑动
通过 Vue 的响应式数据和 CSS 过渡效果实现:
<template>
<div class="slider-container">
<div class="slider" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
<div class="slide" v-for="(image, index) in images" :key="index">
<img :src="image" alt="Slide">
</div>
</div>
<button @click="prev">Previous</button>
<button @click="next">Next</button>
</div>
</template>
<script>
export default {
data() {
return {
images: [
'image1.jpg',
'image2.jpg',
'image3.jpg'
],
currentIndex: 0
};
},
methods: {
prev() {
this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length;
},
next() {
this.currentIndex = (this.currentIndex + 1) % this.images.length;
}
}
};
</script>
<style scoped>
.slider-container {
width: 100%;
height: 300px;
overflow: hidden;
position: relative;
}
.slider {
display: flex;
transition: transform 0.5s ease;
}
.slide {
min-width: 100%;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
添加触摸事件支持
扩展手动实现方案以支持触摸滑动:
<template>
<div
class="slider-container"
@touchstart="handleTouchStart"
@touchmove="handleTouchMove"
@touchend="handleTouchEnd"
>
<div class="slider" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
<div class="slide" v-for="(image, index) in images" :key="index">
<img :src="image" alt="Slide">
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
images: [
'image1.jpg',
'image2.jpg',
'image3.jpg'
],
currentIndex: 0,
touchStartX: 0,
touchEndX: 0
};
},
methods: {
handleTouchStart(e) {
this.touchStartX = e.changedTouches[0].screenX;
},
handleTouchMove(e) {
this.touchEndX = e.changedTouches[0].screenX;
},
handleTouchEnd() {
if (this.touchStartX - this.touchEndX > 50) {
this.next();
}
if (this.touchEndX - this.touchStartX > 50) {
this.prev();
}
},
prev() {
this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length;
},
next() {
this.currentIndex = (this.currentIndex + 1) % this.images.length;
}
}
};
</script>
自动轮播功能
为手动实现方案添加自动轮播:
<script>
export default {
data() {
return {
images: [
'image1.jpg',
'image2.jpg',
'image3.jpg'
],
currentIndex: 0,
interval: null
};
},
mounted() {
this.startAutoPlay();
},
beforeDestroy() {
this.stopAutoPlay();
},
methods: {
startAutoPlay() {
this.interval = setInterval(() => {
this.next();
}, 3000);
},
stopAutoPlay() {
clearInterval(this.interval);
},
next() {
this.currentIndex = (this.currentIndex + 1) % this.images.length;
}
}
};
</script>






