当前位置:首页 > VUE

vue实现slideup效果

2026-01-19 21:18:05VUE

实现 SlideUp 效果的 Vue 方法

使用 CSS Transition 和 Vue 指令

通过 Vue 的 v-showv-if 结合 CSS Transition 实现滑动效果。定义一个 CSS 类控制元素高度和过渡效果。

vue实现slideup效果

<template>
  <div>
    <button @click="toggle">Toggle Slide</button>
    <div class="slide-container" v-show="isVisible">
      <div class="slide-content">
        Content to slide up/down
      </div>
    </div>
  </div>
</template>

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

<style>
.slide-container {
  overflow: hidden;
  transition: max-height 0.5s ease;
  max-height: 0;
}

.slide-container.show {
  max-height: 500px; /* 调整为实际内容高度 */
}

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

使用 Vue Transition 组件

Vue 内置的 <transition> 组件可以更灵活地控制动画效果。结合 CSS 定义滑动动画。

vue实现slideup效果

<template>
  <div>
    <button @click="toggle">Toggle Slide</button>
    <transition name="slide">
      <div v-show="isVisible" class="slide-content">
        Content to slide up/down
      </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: all 0.5s ease;
  max-height: 500px;
}

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

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

使用第三方库(如 Velocity.js)

对于更复杂的动画效果,可以集成 Velocity.js 或其他动画库。通过 Vue 的 transition 组件钩子实现。

<template>
  <div>
    <button @click="toggle">Toggle Slide</button>
    <transition
      @enter="enter"
      @leave="leave"
      :css="false"
    >
      <div v-show="isVisible" class="slide-content">
        Content to slide up/down
      </div>
    </transition>
  </div>
</template>

<script>
import Velocity from 'velocity-animate';

export default {
  data() {
    return {
      isVisible: false
    };
  },
  methods: {
    toggle() {
      this.isVisible = !this.isVisible;
    },
    enter(el, done) {
      Velocity(el, { 
        maxHeight: '500px',
        opacity: 1
      }, { 
        duration: 500,
        complete: done 
      });
    },
    leave(el, done) {
      Velocity(el, { 
        maxHeight: '0',
        opacity: 0
      }, { 
        duration: 500,
        complete: done 
      });
    }
  }
};
</script>

动态计算内容高度

如果需要根据内容动态调整高度,可以通过 JavaScript 计算内容高度并应用到动画中。

<template>
  <div>
    <button @click="toggle">Toggle Slide</button>
    <div class="slide-container" :style="{ height: isVisible ? contentHeight + 'px' : '0' }">
      <div ref="content" class="slide-content">
        Content with dynamic height
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isVisible: false,
      contentHeight: 0
    };
  },
  methods: {
    toggle() {
      this.isVisible = !this.isVisible;
      if (this.isVisible) {
        this.contentHeight = this.$refs.content.scrollHeight;
      }
    }
  }
};
</script>

<style>
.slide-container {
  overflow: hidden;
  transition: height 0.5s ease;
}

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

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

相关文章

vue前端实现注册

vue前端实现注册

使用 Vue 实现用户注册功能 环境准备 确保已安装 Vue CLI 或通过 CDN 引入 Vue。若使用 Vue CLI,需安装 axios 或其他 HTTP 库用于后端通信。 注册表单组件 创…

vue手动实现弹窗

vue手动实现弹窗

实现弹窗组件的基本结构 在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。 <template>…

vue实现菜单调用

vue实现菜单调用

Vue 实现动态菜单的常见方法 基于路由配置生成菜单 Vue Router 的路由配置可以天然转化为菜单结构,适合权限与路由绑定的系统: // router/index.js const rout…

vue实现一个系统

vue实现一个系统

Vue 系统实现步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目: npm init vue@latest my-system 选择需要的配置(Router、Pinia 等)。…

vue设计与实现下载

vue设计与实现下载

vue设计与实现电子书下载 《Vue.js设计与实现》是一本深入解析Vue.js框架原理的书籍,由霍春阳(HcySunYang)撰写。以下是获取该资源的常见途径: 正版购买渠道 京东、当当、天…

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…