当前位置:首页 > VUE

vue实现slidedown

2026-01-13 08:16:40VUE

Vue 实现 SlideDown 效果

SlideDown 是一种常见的动画效果,用于平滑地展开元素。在 Vue 中可以通过多种方式实现,以下是几种常见的方法:

使用 CSS Transition 实现

通过 Vue 的 v-ifv-show 控制元素的显示与隐藏,结合 CSS Transition 实现动画效果。

<template>
  <div>
    <button @click="toggle">Toggle Slide</button>
    <transition name="slide">
      <div v-show="isVisible" class="content">
        This content will slide down and up.
      </div>
    </transition>
  </div>
</template>

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

<style>
.slide-enter-active, .slide-leave-active {
  transition: max-height 0.5s ease;
  overflow: hidden;
}

.slide-enter, .slide-leave-to {
  max-height: 0;
}

.slide-enter-to, .slide-leave {
  max-height: 1000px;
}

.content {
  background: #f0f0f0;
  padding: 10px;
}
</style>

使用 Vue Transition 钩子

通过 JavaScript 钩子函数实现更复杂的动画控制。

<template>
  <div>
    <button @click="toggle">Toggle Slide</button>
    <transition
      @before-enter="beforeEnter"
      @enter="enter"
      @leave="leave"
    >
      <div v-show="isVisible" class="content">
        This content will slide down and up.
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isVisible: false
    };
  },
  methods: {
    toggle() {
      this.isVisible = !this.isVisible;
    },
    beforeEnter(el) {
      el.style.height = '0';
    },
    enter(el) {
      el.style.height = `${el.scrollHeight}px`;
    },
    leave(el) {
      el.style.height = '0';
    }
  }
};
</script>

<style>
.content {
  transition: height 0.5s ease;
  overflow: hidden;
  background: #f0f0f0;
  padding: 10px;
}
</style>

使用第三方库(如 VueUse)

利用 @vueuse/core 提供的 useTransition 实现更平滑的动画效果。

<template>
  <div>
    <button @click="toggle">Toggle Slide</button>
    <div ref="content" class="content">
      This content will slide down and up.
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';
import { useTransition } from '@vueuse/core';

export default {
  setup() {
    const content = ref(null);
    const isVisible = ref(false);
    const height = useTransition(
      () => (isVisible.value ? content.value.scrollHeight : 0),
      { duration: 500 }
    );

    const toggle = () => {
      isVisible.value = !isVisible.value;
    };

    return { content, height, toggle };
  }
};
</script>

<style>
.content {
  overflow: hidden;
  background: #f0f0f0;
  padding: 10px;
}
</style>

使用 Tailwind CSS 实现

如果项目中使用了 Tailwind CSS,可以结合其 Transition 类快速实现 SlideDown 效果。

vue实现slidedown

<template>
  <div>
    <button @click="toggle" class="bg-blue-500 text-white p-2 rounded">
      Toggle Slide
    </button>
    <div
      class="overflow-hidden transition-all duration-500 ease-in-out"
      :style="{ maxHeight: isVisible ? '1000px' : '0' }"
    >
      <div class="bg-gray-100 p-4">
        This content will slide down and up.
      </div>
    </div>
  </div>
</template>

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

以上方法均可实现 SlideDown 效果,选择适合项目需求和技术栈的方案即可。

标签: vueslidedown
分享给朋友:

相关文章

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…

实现vue组件

实现vue组件

Vue 组件的基本实现 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种方式: 单文件组件 (SFC) 使用 .vue 文件格式,…

vue实现类别

vue实现类别

Vue 实现类别的几种方法 在 Vue 中实现类别功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind:class 动态绑定类名 通过对象语法或数组语法动态绑定类名,根据条件添加或移…

vue实现全屏

vue实现全屏

Vue 实现全屏功能的方法 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 来实现。以下是几种常见的实现方式: 使用原生 Fullscreen API 通过调用 doc…

vue实现购票

vue实现购票

Vue 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…