vue实现横向滑动
Vue 实现横向滑动的方法
使用 CSS 和 Vue 结合实现横向滚动
通过 CSS 的 overflow-x 和 white-space 属性,结合 Vue 的动态数据绑定,可以轻松实现横向滑动效果。
<template>
<div class="horizontal-scroll-container">
<div class="horizontal-scroll-content">
<div v-for="item in items" :key="item.id" class="scroll-item">
{{ item.text }}
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
items: [
{ id: 1, text: 'Item 1' },
{ id: 2, text: 'Item 2' },
{ id: 3, text: 'Item 3' },
{ id: 4, text: 'Item 4' },
{ id: 5, text: 'Item 5' },
],
};
},
};
</script>
<style>
.horizontal-scroll-container {
width: 100%;
overflow-x: auto;
white-space: nowrap;
}
.horizontal-scroll-content {
display: inline-block;
}
.scroll-item {
display: inline-block;
width: 200px;
height: 100px;
margin-right: 10px;
background-color: #f0f0f0;
text-align: center;
line-height: 100px;
}
</style>
使用第三方库实现更复杂的横向滑动
对于更复杂的横向滑动需求,可以使用第三方库如 vue-horizontal-scroll 或 swiper。
安装 vue-horizontal-scroll:
npm install vue-horizontal-scroll
示例代码:
<template>
<vue-horizontal>
<div v-for="item in items" :key="item.id" class="item">
{{ item.text }}
</div>
</vue-horizontal>
</template>
<script>
import VueHorizontal from "vue-horizontal-scroll";
export default {
components: { VueHorizontal },
data() {
return {
items: [
{ id: 1, text: 'Item 1' },
{ id: 2, text: 'Item 2' },
{ id: 3, text: 'Item 3' },
{ id: 4, text: 'Item 4' },
{ id: 5, text: 'Item 5' },
],
};
},
};
</script>
<style>
.item {
width: 200px;
height: 100px;
margin-right: 10px;
background-color: #f0f0f0;
text-align: center;
line-height: 100px;
}
</style>
使用 Swiper 实现横向滑动轮播
Swiper 是一个功能强大的滑动库,支持横向滑动、轮播、分页等功能。

安装 Swiper:
npm install swiper vue-awesome-swiper
示例代码:
<template>
<swiper :options="swiperOptions">
<swiper-slide v-for="item in items" :key="item.id">
{{ item.text }}
</swiper-slide>
</swiper>
</template>
<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper';
import 'swiper/css/swiper.css';
export default {
components: {
Swiper,
SwiperSlide,
},
data() {
return {
items: [
{ id: 1, text: 'Item 1' },
{ id: 2, text: 'Item 2' },
{ id: 3, text: 'Item 3' },
{ id: 4, text: 'Item 4' },
{ id: 5, text: 'Item 5' },
],
swiperOptions: {
slidesPerView: 'auto',
spaceBetween: 10,
freeMode: true,
},
};
},
};
</script>
<style>
.swiper-slide {
width: 200px;
height: 100px;
background-color: #f0f0f0;
text-align: center;
line-height: 100px;
}
</style>
响应式设计考虑
为了在不同设备上都能良好显示横向滑动内容,可以通过媒体查询调整样式。

@media (max-width: 768px) {
.horizontal-scroll-container {
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
}
性能优化
对于大量数据的横向滑动,可以使用虚拟滚动技术,如 vue-virtual-scroller,以减少 DOM 节点数量,提升性能。
安装 vue-virtual-scroller:
npm install vue-virtual-scroller
示例代码:
<template>
<RecycleScroller
class="scroller"
:items="items"
:item-size="200"
direction="horizontal"
key-field="id"
>
<template v-slot="{ item }">
<div class="item">
{{ item.text }}
</div>
</template>
</RecycleScroller>
</template>
<script>
import { RecycleScroller } from 'vue-virtual-scroller';
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css';
export default {
components: { RecycleScroller },
data() {
return {
items: Array.from({ length: 100 }, (_, i) => ({ id: i, text: `Item ${i}` })),
};
},
};
</script>
<style>
.scroller {
width: 100%;
height: 100px;
overflow-x: auto;
}
.item {
display: inline-block;
width: 200px;
height: 100px;
margin-right: 10px;
background-color: #f0f0f0;
text-align: center;
line-height: 100px;
}
</style>






