当前位置:首页 > 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 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…

vue实现微博印象

vue实现微博印象

实现微博印象功能的核心思路 微博印象功能通常指用户发布微博后,系统自动分析文本内容并提取关键词或标签,以可视化形式展示。Vue实现该功能需要结合文本分析、关键词提取和前端渲染技术。 文本分析与关键…

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 在Vue中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件:…

vue实现安全免登录

vue实现安全免登录

Vue 实现安全免登录的方案 免登录功能通常通过持久化认证状态(如 Token)实现,但需兼顾安全性。以下是基于 Vue 的常见实现方案: 基于 Token 的免登录 登录成功后存储 Toke…

vue实现冰墩墩

vue实现冰墩墩

使用 Vue 实现冰墩墩动画 通过 Vue 的动画特性和 SVG 技术,可以动态渲染冰墩墩的形象。以下是具体实现方法: 准备 SVG 素材 冰墩墩的形象通常由多个 SVG 路径组成,包括身体轮廓…

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <tem…