当前位置:首页 > 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-if 或 v-show 控制内容的显示与隐藏。初始状态下只显示部分内容,点击“查看更多”后展开完整内容。 <template&…

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用…

vue实现mvc

vue实现mvc

Vue 实现 MVC 模式 Vue 本身是一个 MVVM(Model-View-ViewModel)框架,但可以通过结构调整实现 MVC(Model-View-Controller)模式。以下是具体实…

vue 实现toast

vue 实现toast

vue 实现 toast 的方法 在 Vue 中实现 Toast 提示功能可以通过多种方式,以下是几种常见的实现方法: 使用第三方库 Vue 生态中有许多成熟的 Toast 库,例如 vue-toa…

vue实现autocomplete

vue实现autocomplete

Vue 实现 Autocomplete 功能 Vue 中实现 Autocomplete(自动完成)功能可以通过自定义组件或使用现有库完成。以下是几种常见方法: 方法一:使用 Vue 原生实现…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-c…