当前位置:首页 > VUE

vue实现内容左右滑动

2026-02-24 23:25:18VUE

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

使用 CSS 和 touch 事件

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

vue实现内容左右滑动

<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 组件。

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.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实现树

vue实现树

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

vue实现路径

vue实现路径

Vue 实现路径的方法 在 Vue 中实现路径管理通常涉及 Vue Router 的使用,以下是一些常见的实现方法: 安装 Vue Router 通过 npm 或 yarn 安装 Vue Route…

vue实现login

vue实现login

Vue 实现登录功能 在 Vue 中实现登录功能通常需要以下几个步骤: 创建登录表单组件 在 Vue 项目中创建一个登录表单组件,包含用户名和密码输入框以及提交按钮。可以使用 Vue 的模板语法和双…

vue实现页面定位

vue实现页面定位

Vue 实现页面定位的方法 在 Vue 中实现页面定位通常可以通过以下几种方式完成,包括使用原生 JavaScript 的 scrollIntoView 方法、Vue Router 的滚动行为配置,以…

vue实现图片循环

vue实现图片循环

实现图片循环的基本方法 在Vue中实现图片循环通常使用v-for指令结合数组数据。以下是基础实现方式: <template> <div> <img v-fo…

实现vue模板解析

实现vue模板解析

Vue 模板解析的基本原理 Vue 的模板解析是将模板字符串转换为渲染函数的过程。核心步骤包括模板编译、AST 生成、优化和代码生成。 模板编译阶段 Vue 使用 vue-template-comp…