当前位置:首页 > VUE

vue实现slidedown

2026-01-13 08:16:40VUE

Vue 实现 SlideDown 效果

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

使用 CSS Transition 实现

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

vue实现slidedown

<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 钩子函数实现更复杂的动画控制。

vue实现slidedown

<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 效果。

<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中实现搜索功能通常涉及以下几个关键步骤,结合数据绑定、计算属性和方法调用来动态过滤和显示结果。 数据绑定与输入处理 使用v-model双向绑定搜索输入框的值,监听用户…

vue登录业务的实现

vue登录业务的实现

实现登录业务的基本流程 在Vue中实现登录业务通常涉及前端界面设计、表单验证、API请求交互、状态管理以及路由控制等环节。以下是核心实现步骤: 登录表单设计与验证 创建包含用户名和密码输入框的登录组…

vue tag实现

vue tag实现

Vue 标签实现方法 在 Vue 中实现标签功能可以通过多种方式完成,常见场景包括动态标签生成、标签输入框、标签管理等。以下是几种典型实现方案: 动态标签列表渲染 使用 v-for 指令渲染标签数组…

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue实现滑块

vue实现滑块

Vue 实现滑块组件的方法 使用原生 HTML 和 Vue 指令 通过 Vue 的 v-model 和事件监听实现基础滑块功能。创建一个包含 input 元素的组件,类型设置为 range,并绑定到…

vue vnode实现

vue vnode实现

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