当前位置:首页 > VUE

vue实现滑动div

2026-03-09 06:52:25VUE

Vue 实现滑动 Div 的方法

使用 CSS 过渡和 Vue 事件

通过 Vue 的 @touchstart@touchmove@touchend 事件结合 CSS 过渡效果实现滑动效果。需要计算触摸起始位置和移动距离,动态更新 Div 的位置。

<template>
  <div 
    class="slider"
    @touchstart="startDrag"
    @touchmove="onDrag"
    @touchend="endDrag"
    :style="{ transform: `translateX(${currentX}px)` }"
  >
    可滑动内容
  </div>
</template>

<script>
export default {
  data() {
    return {
      startX: 0,
      currentX: 0,
      isDragging: false
    };
  },
  methods: {
    startDrag(e) {
      this.startX = e.touches[0].clientX - this.currentX;
      this.isDragging = true;
    },
    onDrag(e) {
      if (this.isDragging) {
        this.currentX = e.touches[0].clientX - this.startX;
      }
    },
    endDrag() {
      this.isDragging = false;
    }
  }
};
</script>

<style>
.slider {
  transition: transform 0.3s ease;
  touch-action: none;
}
</style>

使用第三方库(如 vue-draggable)

vue-draggable 是一个基于 Sortable.js 的 Vue 拖拽库,可以快速实现滑动和拖拽功能。安装后直接使用组件即可。

安装:

npm install vuedraggable

示例代码:

<template>
  <draggable v-model="items" @start="drag=true" @end="drag=false">
    <div v-for="item in items" :key="item.id">
      {{ item.name }}
    </div>
  </draggable>
</template>

<script>
import draggable from 'vuedraggable';
export default {
  components: { draggable },
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ]
    };
  }
};
</script>

使用 CSS Scroll Snap

如果需求是横向滑动浏览内容,可以使用 CSS 的 scroll-snap 特性实现流畅的滑动效果。这种方法不需要 JavaScript,纯 CSS 实现。

<template>
  <div class="slider-container">
    <div class="slider">
      <div class="slide">Slide 1</div>
      <div class="slide">Slide 2</div>
      <div class="slide">Slide 3</div>
    </div>
  </div>
</template>

<style>
.slider-container {
  overflow-x: auto;
  scroll-snap-type: x mandatory;
}
.slider {
  display: flex;
  width: max-content;
}
.slide {
  scroll-snap-align: start;
  width: 100vw;
  flex-shrink: 0;
}
</style>

使用 Swiper.js

Swiper.js 是一个强大的滑动库,支持 Vue 封装。适用于复杂的滑动需求,如轮播、缩略图导航等。

安装:

npm install swiper vue-awesome-swiper

示例代码:

vue实现滑动div

<template>
  <swiper :options="swiperOptions">
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
  </swiper>
</template>

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

export default {
  components: { Swiper, SwiperSlide },
  data() {
    return {
      swiperOptions: {
        slidesPerView: 1,
        spaceBetween: 30
      }
    };
  }
};
</script>

以上方法可以根据具体需求选择,从简单的 CSS 过渡到复杂的第三方库均可实现滑动效果。

标签: vuediv
分享给朋友:

相关文章

vue实现单词反转

vue实现单词反转

实现思路 在Vue中实现单词反转功能,可以通过多种方式完成。核心思路是利用JavaScript的字符串处理方法,将输入的单词或句子拆分为数组,反转数组后再拼接成字符串。 方法一:使用计算属性 通过计…

vue实现选择季度

vue实现选择季度

Vue 实现选择季度的几种方法 使用下拉选择框(Select) 在 Vue 中可以通过 v-model 绑定一个下拉选择框来实现季度选择。数据可以预先定义为一个包含季度选项的数组。 <temp…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue 实现闪烁

vue 实现闪烁

Vue 实现闪烁效果的方法 方法一:使用 CSS 动画 在 Vue 组件中定义一个闪烁的 CSS 动画,通过绑定 class 或 style 来控制元素的闪烁效果。 <template>…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue observer实现

vue observer实现

Vue Observer 实现原理 Vue 的响应式系统核心是通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)实现的 Observer 模式。以下是关键实现细…