当前位置:首页 > VUE

vue实现滑动

2026-01-11 21:02:58VUE

Vue 实现滑动效果的方法

使用 CSS 过渡和动画

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

<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 等库。

<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 事件实现滑动交互。

vue实现滑动

<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实现tag

vue实现tag

Vue 实现标签(Tag)功能 在 Vue 中实现标签(Tag)功能可以通过多种方式完成,以下是一些常见的方法和实现步骤: 使用动态组件和 v-for 通过 v-for 指令动态渲染标签列表,结合样…

vue实现批量

vue实现批量

Vue 实现批量操作的方法 在 Vue 中实现批量操作通常涉及选择多个项目并执行统一处理,例如批量删除、批量更新等。以下是几种常见实现方式: 表格多选批量操作 使用 el-table 配合复选框实现…

vue vnode实现

vue vnode实现

Vue VNode 实现原理 VNode(Virtual Node)是 Vue 的核心概念之一,用于描述真实 DOM 的轻量级虚拟表示。VNode 的实现涉及以下关键点: VNode 的基本结构 V…

vue 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…

vue原理实现

vue原理实现

Vue 原理实现的核心机制 Vue.js 的核心原理基于响应式系统、虚拟 DOM 和模板编译。以下是其核心实现机制的分解: 响应式系统 Vue 使用 Object.defineProperty(Vu…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…