当前位置:首页 > 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.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实现该功能需要结合文本分析、关键词提取和前端渲染技术。 文本分析与关键词…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue实现用户信息删除

vue实现用户信息删除

Vue 实现用户信息删除功能 在 Vue 中实现用户信息删除功能通常涉及前端界面交互和后端 API 调用。以下是实现步骤: 前端界面设计 创建删除按钮或操作项,通常使用 @click 事件触发删除操…

用vue实现滑动输入条

用vue实现滑动输入条

使用 Vue 实现滑动输入条 使用原生 HTML5 input range 实现 在 Vue 模板中直接使用 <input type="range">,通过 v-model 绑定数据:…

vue实现store

vue实现store

Vue 实现 Store 在 Vue 中,可以通过 Vuex 或 Pinia 实现全局状态管理(Store)。以下是两种主流方案的实现方法。 使用 Vuex 实现 Store Vuex 是 Vu…

vue实现muli

vue实现muli

Vue 实现多语言(i18n)支持 在 Vue 项目中实现多语言(i18n)功能,通常使用 vue-i18n 插件。以下是具体实现步骤: 安装 vue-i18n npm install vue-i…