当前位置:首页 > VUE

vue实现滑动

2026-01-11 21:02:58VUE

Vue 实现滑动效果的方法

使用 CSS 过渡和动画

通过 Vue 的过渡类名结合 CSS 实现滑动效果,适用于简单的元素滑动。

vue实现滑动

<template>
  <div>
    <button @click="show = !show">Toggle Slide</button>
    <transition name="slide">
      <div v-if="show" class="box">Slide Content</div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: false
    };
  }
};
</script>

<style>
.slide-enter-active, .slide-leave-active {
  transition: transform 0.5s ease;
}
.slide-enter, .slide-leave-to {
  transform: translateX(100%);
}
.box {
  width: 200px;
  height: 100px;
  background: #42b983;
}
</style>

使用第三方库(如 Swiper.js)

对于复杂的滑动场景(如轮播图),可以使用 Swiper.js 等库。

vue实现滑动

<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide">Slide 1</div>
      <div class="swiper-slide">Slide 2</div>
      <div class="swiper-slide">Slide 3</div>
    </div>
    <div class="swiper-pagination"></div>
  </div>
</template>

<script>
import Swiper from 'swiper';
import 'swiper/css/swiper.min.css';

export default {
  mounted() {
    new Swiper('.swiper-container', {
      pagination: {
        el: '.swiper-pagination',
      },
    });
  }
};
</script>

自定义指令实现拖动滑动

通过 Vue 自定义指令实现元素的拖动滑动效果。

<template>
  <div v-drag class="draggable" :style="{ left: position.x + 'px', top: position.y + 'px' }">
    Drag Me
  </div>
</template>

<script>
export default {
  data() {
    return {
      position: { x: 0, y: 0 }
    };
  },
  directives: {
    drag: {
      bind(el, binding, vnode) {
        let startX, startY, initialX, initialY;
        el.addEventListener('mousedown', startDrag);

        function startDrag(e) {
          startX = e.clientX;
          startY = e.clientY;
          initialX = vnode.context.position.x;
          initialY = vnode.context.position.y;
          document.addEventListener('mousemove', drag);
          document.addEventListener('mouseup', stopDrag);
        }

        function drag(e) {
          vnode.context.position.x = initialX + e.clientX - startX;
          vnode.context.position.y = initialY + e.clientY - startY;
        }

        function stopDrag() {
          document.removeEventListener('mousemove', drag);
          document.removeEventListener('mouseup', stopDrag);
        }
      }
    }
  }
};
</script>

<style>
.draggable {
  width: 100px;
  height: 100px;
  background: #42b983;
  position: absolute;
  cursor: move;
}
</style>

使用 Touch 事件实现移动端滑动

针对移动端,可以通过 Touch 事件实现滑动交互。

<template>
  <div 
    class="touch-area"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
    :style="{ transform: `translateX(${offsetX}px)` }"
  >
    Slide Content
  </div>
</template>

<script>
export default {
  data() {
    return {
      startX: 0,
      offsetX: 0
    };
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX;
    },
    handleTouchMove(e) {
      const currentX = e.touches[0].clientX;
      this.offsetX = currentX - this.startX;
    },
    handleTouchEnd() {
      this.offsetX = 0;
    }
  }
};
</script>

<style>
.touch-area {
  width: 200px;
  height: 100px;
  background: #42b983;
  touch-action: none;
}
</style>

标签: vue
分享给朋友:

相关文章

vue实现弹窗

vue实现弹窗

Vue 实现弹窗的常见方法 使用组件化方式封装弹窗 创建一个独立的弹窗组件(如 Modal.vue),通过 v-if 或 v-show 控制显示隐藏: <template> <…

vue组件实现

vue组件实现

Vue 组件实现基础 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

vue computed 实现

vue computed 实现

computed 的基本用法 在 Vue 中,computed 用于声明计算属性,这些属性基于响应式依赖进行缓存。只有当依赖发生变化时,计算属性才会重新计算。 new Vue({ data: {…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue插件实现

vue插件实现

Vue 插件实现方法 Vue 插件是一种向 Vue 应用程序添加全局功能的机制。插件可以包含全局指令、过滤器、混入、实例方法等。 插件的基本结构 一个 Vue 插件通常是一个对象或函数,需要暴露一个…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装 v…