当前位置:首页 > 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 中实现拖放功能通常可以通过 HTML5 的原生拖放 API 或第三方库(如 vuedraggable)来实现。以下是两种方法的详细说明。 使用 HTML5 原生拖放 A…

简单实现vue

简单实现vue

Vue 的基本实现 创建一个简单的 Vue 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &l…

vue的实现

vue的实现

Vue 的实现原理 Vue 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是 Vue 实现的主要技术细节。 响应式系统 Vue 的响…

vue实现抽奖

vue实现抽奖

Vue 实现抽奖功能 基本抽奖逻辑 创建一个Vue组件,包含奖品列表、抽奖按钮和结果显示区域。使用随机数生成器选择奖品。 <template> <div> <…

vue 实现modal

vue 实现modal

Vue 实现 Modal 的方法 使用 Vue 原生组件 通过 Vue 的组件系统可以轻松实现 Modal。创建一个可复用的 Modal 组件,通过 props 控制显示与隐藏。 <templ…

vue 简单实现

vue 简单实现

以下是 Vue 的简单实现示例,涵盖基础模板、数据绑定、方法及生命周期: 基础模板与数据绑定 <div id="app"> <p>{{ message }}</p&…