当前位置:首页 > VUE

vue 实现滑动效果

2026-01-17 16:08:44VUE

Vue 实现滑动效果的方法

使用 CSS Transition

通过 Vue 的 v-ifv-show 结合 CSS Transition 实现滑动效果。定义一个 CSS 类,利用 transformmargin 属性实现平滑过渡。

vue 实现滑动效果

<template>
  <div>
    <button @click="toggle">Toggle Slide</button>
    <transition name="slide">
      <div v-if="show" class="box">滑动内容</div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: false
    };
  },
  methods: {
    toggle() {
      this.show = !this.show;
    }
  }
};
</script>

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

使用 Vue Transition 的 JavaScript 钩子

通过 JavaScript 钩子(如 enterleave)结合动画库(如 GSAP 或 Anime.js)实现更复杂的滑动效果。

vue 实现滑动效果

<template>
  <div>
    <button @click="toggle">Toggle Slide</button>
    <transition
      @enter="enter"
      @leave="leave"
      :css="false"
    >
      <div v-if="show" class="box">滑动内容</div>
    </transition>
  </div>
</template>

<script>
import gsap from 'gsap';
export default {
  data() {
    return {
      show: false
    };
  },
  methods: {
    toggle() {
      this.show = !this.show;
    },
    enter(el, done) {
      gsap.from(el, {
        x: 100,
        opacity: 0,
        duration: 0.5,
        onComplete: done
      });
    },
    leave(el, done) {
      gsap.to(el, {
        x: 100,
        opacity: 0,
        duration: 0.5,
        onComplete: done
      });
    }
  }
};
</script>

使用第三方库(如 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>
</template>

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

使用 Vue 的 <transition-group>

对于列表项的滑动效果,可以使用 <transition-group> 实现多个元素的动画。

<template>
  <div>
    <button @click="addItem">Add Item</button>
    <button @click="removeItem">Remove Item</button>
    <transition-group name="list" tag="ul">
      <li v-for="item in items" :key="item" class="list-item">
        {{ item }}
      </li>
    </transition-group>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [1, 2, 3],
      nextNum: 4
    };
  },
  methods: {
    addItem() {
      this.items.push(this.nextNum++);
    },
    removeItem() {
      this.items.pop();
    }
  }
};
</script>

<style>
.list-item {
  display: inline-block;
  margin-right: 10px;
}
.list-enter-active, .list-leave-active {
  transition: all 1s;
}
.list-enter, .list-leave-to {
  opacity: 0;
  transform: translateY(30px);
}
</style>

以上方法可根据需求选择,CSS Transition 适合简单滑动,JavaScript 钩子适合复杂动画,Swiper.js 适合轮播,<transition-group> 适合列表动画。

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

相关文章

vue双向实现

vue双向实现

Vue 双向数据绑定的实现原理 Vue 的双向数据绑定主要通过 v-model 指令实现,其核心是结合了数据劫持(Object.defineProperty 或 Proxy)和发布-订阅模式。 数…

vue怎么实现选中删除

vue怎么实现选中删除

实现选中删除功能的方法 在Vue中实现选中删除功能通常涉及以下几个关键步骤: 数据绑定与选中状态管理 使用v-model或v-for指令绑定列表数据,结合复选框或单选按钮实现选中状态管理。例如…

vue前端分页怎么实现

vue前端分页怎么实现

前端分页实现方法 在Vue中实现前端分页通常涉及数据分割和页面控制逻辑。以下是常见的实现方式: 使用计算属性分页 通过计算属性对数据进行切片处理: computed: { paginate…

vue异步组建实现原理

vue异步组建实现原理

Vue 异步组件实现原理 Vue 的异步组件允许将组件按需加载,通常用于代码分割或延迟加载非关键组件。其核心原理基于 Promise 和动态导入。 基本实现方式 通过 defineAsyncComp…

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store…

vue无限菜单怎么实现

vue无限菜单怎么实现

实现 Vue 无限菜单的方法 递归组件实现嵌套菜单 使用 Vue 的递归组件特性可以轻松实现无限级嵌套菜单。通过组件调用自身的方式处理嵌套数据。 <template> <ul…