当前位置:首页 > VUE

vue实现内容左右滑动

2026-02-24 23:25:18VUE

Vue 实现内容左右滑动的方法

使用 CSS 和 touch 事件

通过 CSS 的 overflow-x: auto 和 touch 事件实现左右滑动效果。

<template>
  <div class="scroll-container" ref="scrollContainer" @touchstart="handleTouchStart" @touchmove="handleTouchMove">
    <div class="scroll-content">
      <!-- 你的内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      startX: 0,
      scrollLeft: 0
    }
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].pageX
      this.scrollLeft = this.$refs.scrollContainer.scrollLeft
    },
    handleTouchMove(e) {
      const x = e.touches[0].pageX
      const walk = (x - this.startX) * 2
      this.$refs.scrollContainer.scrollLeft = this.scrollLeft - walk
    }
  }
}
</script>

<style>
.scroll-container {
  overflow-x: auto;
  white-space: nowrap;
  -webkit-overflow-scrolling: touch;
}
.scroll-content {
  display: inline-block;
}
</style>

使用第三方库(如 Swiper)

安装 Swiper 库并配置 Vue 组件。

npm install swiper
<template>
  <swiper :options="swiperOptions">
    <swiper-slide v-for="(item, index) in items" :key="index">
      <!-- 你的内容 -->
    </swiper-slide>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/swiper-bundle.css'

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      swiperOptions: {
        slidesPerView: 'auto',
        spaceBetween: 10,
        freeMode: true
      },
      items: [/* 你的数据 */]
    }
  }
}
</script>

使用 CSS Scroll Snap

利用 CSS 的 scroll-snap 特性实现平滑滑动。

<template>
  <div class="scroll-snap-container">
    <div class="scroll-snap-content" v-for="(item, index) in items" :key="index">
      <!-- 你的内容 -->
    </div>
  </div>
</template>

<style>
.scroll-snap-container {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
}
.scroll-snap-content {
  scroll-snap-align: start;
  flex: 0 0 auto;
  width: 80%;
  margin-right: 10px;
}
</style>

使用 Vue 指令实现

自定义 Vue 指令处理滑动逻辑。

vue实现内容左右滑动

Vue.directive('horizontal-scroll', {
  inserted(el) {
    el.addEventListener('touchstart', handleTouchStart)
    el.addEventListener('touchmove', handleTouchMove)

    let startX, scrollLeft

    function handleTouchStart(e) {
      startX = e.touches[0].pageX
      scrollLeft = el.scrollLeft
    }

    function handleTouchMove(e) {
      const x = e.touches[0].pageX
      const walk = (x - startX) * 2
      el.scrollLeft = scrollLeft - walk
    }
  }
})
<template>
  <div v-horizontal-scroll class="scroll-container">
    <!-- 你的内容 -->
  </div>
</template>

标签: 内容vue
分享给朋友:

相关文章

vue实现triger

vue实现triger

在Vue中实现触发器(trigger)功能通常涉及自定义事件或DOM事件触发。以下是几种常见场景的实现方法: 自定义事件触发 通过$emit方法触发父组件中监听的自定义事件: // 子组件 thi…

vue 路由 实现

vue 路由 实现

Vue 路由实现方法 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是实现 Vue 路由的详细步骤: 安装 Vue Router 通过 npm 或 ya…

vue树形实现

vue树形实现

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

简单实现vue

简单实现vue

Vue 的基本实现 创建一个简单的 Vue 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &l…

vue vnode实现

vue vnode实现

Vue VNode 实现原理 VNode(Virtual Node)是 Vue 的核心概念之一,用于描述真实 DOM 的轻量级虚拟表示。VNode 的实现涉及以下关键点: VNode 的基本结构 V…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templat…