当前位置:首页 > VUE

vue实现列表左右滑动

2026-02-22 19:42:41VUE

实现列表左右滑动的方法

在Vue中实现列表左右滑动通常可以通过以下方法完成,具体取决于项目需求和技术栈。

使用CSS和Touch事件

通过CSS的overflow-xtouch事件实现简单滑动效果。

<template>
  <div class="slider-container" @touchstart="handleTouchStart" @touchmove="handleTouchMove">
    <div class="slider-content" :style="{ transform: `translateX(${offsetX}px)` }">
      <div v-for="item in items" :key="item.id" class="slider-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' },
      ],
      startX: 0,
      offsetX: 0,
    };
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX;
    },
    handleTouchMove(e) {
      const moveX = e.touches[0].clientX - this.startX;
      this.offsetX += moveX;
      this.startX = e.touches[0].clientX;
    },
  },
};
</script>

<style>
.slider-container {
  overflow-x: auto;
  white-space: nowrap;
  width: 100%;
  touch-action: pan-y;
}
.slider-content {
  display: inline-flex;
  transition: transform 0.3s ease;
}
.slider-item {
  width: 100px;
  height: 100px;
  margin-right: 10px;
  background: #eee;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
</style>

使用第三方库(如Swiper)

Swiper是一个流行的滑动库,支持Vue集成,提供丰富的配置和效果。

<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 'swiper/vue';
import 'swiper/swiper-bundle.css';

export default {
  components: {
    Swiper,
    SwiperSlide,
  },
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' },
        { id: 3, text: 'Item 3' },
      ],
      swiperOptions: {
        slidesPerView: 'auto',
        spaceBetween: 10,
        freeMode: true,
      },
    };
  },
};
</script>

<style>
.swiper-slide {
  width: 100px;
  height: 100px;
  background: #eee;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>

使用Vue专用滑动组件(如vue-slick)

vue-slick是另一个轻量级滑动组件,适合简单需求。

<template>
  <slick :options="slickOptions">
    <div v-for="item in items" :key="item.id">
      {{ item.text }}
    </div>
  </slick>
</template>

<script>
import Slick from 'vue-slick';

export default {
  components: { Slick },
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' },
        { id: 3, text: 'Item 3' },
      ],
      slickOptions: {
        slidesToShow: 3,
        slidesToScroll: 1,
        infinite: false,
      },
    };
  },
};
</script>

<style>
.slick-slide {
  width: 100px;
  height: 100px;
  background: #eee;
  margin: 0 10px;
  display: flex !important;
  align-items: center;
  justify-content: center;
}
</style>

使用CSS Scroll Snap

现代CSS的scroll-snap属性可以轻松实现滑动效果,无需JavaScript。

vue实现列表左右滑动

<template>
  <div class="scroll-container">
    <div v-for="item in items" :key="item.id" class="scroll-item">
      {{ item.text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' },
        { id: 3, text: 'Item 3' },
      ],
    };
  },
};
</script>

<style>
.scroll-container {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  gap: 10px;
  padding: 10px 0;
}
.scroll-item {
  scroll-snap-align: start;
  min-width: 100px;
  height: 100px;
  background: #eee;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
</style>

注意事项

  • 性能优化:对于长列表,考虑虚拟滚动(如vue-virtual-scroller)避免渲染过多DOM节点。
  • 浏览器兼容性:CSS Scroll Snap和Touch事件需测试目标浏览器支持情况。
  • 手势冲突:确保滑动区域不会与页面其他手势操作冲突。

标签: 列表vue
分享给朋友:

相关文章

vue实现语音

vue实现语音

Vue 实现语音功能的方法 使用 Web Speech API Vue 可以结合 Web Speech API 实现语音识别和合成功能。Web Speech API 包含 SpeechRecognit…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue实现节流

vue实现节流

vue实现节流的方法 在Vue中实现节流(throttle)功能,通常用于限制高频事件的触发频率,例如滚动、输入或按钮点击。以下是几种常见的实现方式: 使用Lodash的throttle函数 安装L…

vue 实现打印

vue 实现打印

Vue 实现打印功能的方法 在Vue项目中实现打印功能,可以通过以下几种方式实现: 使用window.print()方法 通过调用浏览器的原生打印API实现基础打印功能,适用于简单内容打印。 //…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind:…

vue实现树

vue实现树

Vue 实现树形结构的方法 使用递归组件实现树形结构 递归组件是Vue中实现树形结构的常见方式。通过组件调用自身,可以轻松构建多层嵌套的树形数据。 <template> <ul…