当前位置:首页 > 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 效果。

<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 效果,选择适合项目需求和技术栈的方案即可。

vue实现slidedown

标签: vueslidedown
分享给朋友:

相关文章

vue实现皮肤切换

vue实现皮肤切换

实现皮肤切换的基本思路 在Vue中实现皮肤切换功能,通常涉及动态修改CSS变量或类名。核心是通过状态管理当前主题,并在组件中应用对应的样式。 使用CSS变量实现主题切换 CSS变量(自定义属性)是…

vue实现图集

vue实现图集

Vue 实现图集的方法 在 Vue 中实现图集功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用第三方库(如 vue-image-lightbox) 安装 vue-image-ligh…

vue实现switch

vue实现switch

Vue 实现 Switch 开关组件 在 Vue 中实现 Switch 开关组件可以通过自定义组件或使用现有 UI 库完成。以下是几种常见实现方式: 自定义 Switch 组件 创建一个基础 Sw…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <…

vue搜索实现

vue搜索实现

Vue 搜索功能实现方法 基于计算属性的搜索 在 Vue 中可以通过计算属性实现简单的搜索功能。这种方式适合小型数据集,利用 JavaScript 的 filter 方法进行筛选。 data() {…

vue 实现长按

vue 实现长按

实现长按功能的基本思路 在Vue中实现长按功能,通常需要监听元素的mousedown或touchstart事件,启动一个计时器。当用户按住元素超过设定的时间阈值时,触发长按回调函数。如果在时间阈值内触…