当前位置:首页 > 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实现ssr

vue实现ssr

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

vue 实现fadeout

vue 实现fadeout

Vue 实现淡出效果 在 Vue 中实现淡出(fade-out)效果可以通过 CSS 过渡或动画结合 Vue 的动态渲染功能完成。以下是几种常见方法: 使用 Vue 过渡(Transition)组件…

vue 实现后退

vue 实现后退

实现后退功能的方法 在Vue中实现后退功能通常需要结合浏览器历史记录API或Vue Router的导航方法。以下是几种常见的实现方式: 使用Vue Router的go方法 this.$router…

vue 实现评分

vue 实现评分

Vue 实现评分功能 在 Vue 中实现评分功能可以通过多种方式完成,包括使用第三方组件库或自定义实现。以下是两种常见的方法: 使用第三方组件库(如 Element UI) Element UI 提…

vue实现兼容

vue实现兼容

Vue 兼容性实现方案 Vue.js 的兼容性主要涉及对不同浏览器、Vue 版本以及第三方库的支持。以下是常见的兼容性解决方案: 浏览器兼容性 使用 Babel 转换 ES6+ 代码为 ES5 语…

vue实现webshell

vue实现webshell

Vue 实现 WebShell 使用 Vue 实现 WebShell 需要结合前端与后端的交互,通常通过 WebSocket 或 HTTP 协议与服务器通信。以下是一个基于 Vue 和 WebSock…