当前位置:首页 > 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。

<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

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script s…

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert 生成…

vue实现erp

vue实现erp

Vue实现ERP系统的关键步骤 技术选型与架构设计 采用Vue 3 + TypeScript构建前端,搭配Pinia状态管理,Element Plus或Ant Design Vue作为UI框架。后端可…

vue筛选实现

vue筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <div…

vue实现必填

vue实现必填

Vue 实现表单必填验证的方法 在Vue中实现表单必填验证可以通过多种方式完成,以下是几种常见的方法: 使用HTML5原生属性 最简单的必填验证可以通过HTML5的required属性实现:…

vue实现ssr

vue实现ssr

Vue 实现 SSR(服务端渲染) Vue 的服务端渲染(SSR)允许在服务器端生成完整的 HTML 内容,再发送到客户端,提升首屏加载速度和 SEO 友好性。以下是实现 Vue SSR 的关键步骤和…