当前位置:首页 > VUE

vue 实现滑动效果

2026-02-18 08:39:47VUE

Vue 实现滑动效果的常见方法

使用 CSS Transition 或 Animation

通过 Vue 的 v-bind:classv-bind:style 动态绑定样式,结合 CSS 的 transitionanimation 属性实现滑动效果。

vue 实现滑动效果

<template>
  <div 
    class="slider" 
    :style="{ transform: `translateX(${offset}px)` }"
  ></div>
</template>

<script>
export default {
  data() {
    return {
      offset: 0
    };
  },
  methods: {
    slide() {
      this.offset += 100; // 每次滑动 100px
    }
  }
};
</script>

<style>
.slider {
  transition: transform 0.3s ease;
  width: 100px;
  height: 100px;
  background: blue;
}
</style>

使用 Vue Transition 组件

Vue 内置的 <transition> 组件可以方便地实现进入/离开的滑动动画。

vue 实现滑动效果

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

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

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

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

对于复杂的滑动场景(如轮播图),可以直接集成 Swiper 等专业库。

<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="item in 5" :key="item">
        Slide {{ item }}
      </div>
    </div>
  </div>
</template>

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

export default {
  mounted() {
    new Swiper('.swiper-container', {
      loop: true,
      autoplay: {
        delay: 3000,
      },
    });
  }
};
</script>

手势滑动(如 hammer.js)

通过手势库实现触摸滑动交互。

<template>
  <div ref="panElement" class="pan-area"></div>
</template>

<script>
import Hammer from 'hammerjs';

export default {
  mounted() {
    const hammer = new Hammer(this.$refs.panElement);
    hammer.on('pan', (event) => {
      this.$refs.panElement.style.transform = `translateX(${event.deltaX}px)`;
    });
  }
};
</script>

<style>
.pan-area {
  width: 200px;
  height: 200px;
  background: green;
  touch-action: none;
}
</style>

关键注意事项

  • 性能优化:对于频繁触发的滑动(如跟随鼠标),使用 requestAnimationFrame 避免卡顿。
  • 移动端适配:触摸事件需添加 touch-action CSS 属性防止浏览器默认行为冲突。
  • 动画流畅性:优先使用 transformopacity 属性,这些属性不会触发重排。

标签: 效果vue
分享给朋友:

相关文章

vue实现摘要

vue实现摘要

Vue 实现摘要的方法 在 Vue 中实现文本摘要功能通常涉及截取文本的前部分内容并添加省略号。可以通过计算属性、过滤器或自定义指令来实现。 计算属性实现 在 Vue 组件中定义一个计算属性,用于截…

vue element实现

vue element实现

Vue Element UI 实现 Vue Element UI 是基于 Vue.js 的组件库,提供丰富的 UI 组件,适合快速开发企业级中后台产品。以下是一些关键实现方法和步骤: 安装 Vue…

vue实现input

vue实现input

Vue 实现 Input 输入框的方法 基本双向绑定 使用 v-model 指令实现双向数据绑定,适用于大多数表单场景: <template> <input v-model="…

vue实现frame

vue实现frame

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

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

vue 实现href

vue 实现href

Vue 实现 href 的方法 在 Vue 中实现 href 功能可以通过多种方式,具体取决于需求场景。以下是几种常见的方法: 使用 <a> 标签 直接使用 HTML 的 <a&g…