SimpleTransition 简单过渡
介绍
简单过渡组件,用于在元素显示和隐藏之间添加过渡效果。仿照Vue官方的Transition,填补了Uniapp没有Transition的空缺。
导入
js
import SimpleTransition from '@/components/anim/SimpleTransition.vue';用法
基础用法
基础用法展示默认配置的过渡效果,通过控制show属性来切换元素的显示状态。
vue
<template>
<FlexCol gap="20">
<Button
text="切换显示/隐藏"
type="primary"
@click="showBasic = !showBasic"
/>
<SimpleTransition :show="showBasic">
<template #show="{ classNames }">
<view :class="['transition-box', ...classNames]">
基础过渡效果
</view>
</template>
</SimpleTransition>
</FlexCol>
</template>
<script setup>
import { ref } from 'vue';
import SimpleTransition from '@/components/anim/SimpleTransition.vue';
import Button from '@/components/basic/Button.vue';
import FlexCol from '@/components/layout/FlexCol.vue';
const showBasic = ref(false);
</script>
<style scoped>
.transition-box {
width: 200px;
height: 100px;
background-color: #409eff;
color: white;
display: flex;
align-items: center;
justify-content: center;
border-radius: 8px;
font-size: 16px;
}
/* 默认动画样式 (name="v") */
.v-enter-active,
.v-leave-active {
transition: opacity 0.5s ease, transform 0.5s ease;
}
.v-enter-from {
opacity: 0;
transform: translateY(-20px);
}
.v-leave-to {
opacity: 0;
transform: translateY(20px);
}
</style>自定义动画名称
通过name属性自定义动画类名前缀,可以实现不同的过渡效果。
vue
<template>
<FlexCol gap="20">
<Button
text="切换显示/隐藏"
type="success"
@click="showCustomName = !showCustomName"
/>
<SimpleTransition :show="showCustomName" name="fade">
<template #show="{ classNames }">
<view :class="['transition-box', 'fade-box', ...classNames]">
自定义淡入淡出效果
</view>
</template>
</SimpleTransition>
</FlexCol>
</template>
<script setup>
import { ref } from 'vue';
import SimpleTransition from '@/components/anim/SimpleTransition.vue';
import Button from '@/components/basic/Button.vue';
import FlexCol from '@/components/layout/FlexCol.vue';
const showCustomName = ref(false);
</script>
<style scoped>
/* 淡入淡出动画样式 */
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.5s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
</style>自定义动画持续时间
通过duration属性设置动画持续时间,控制过渡效果的快慢。
vue
<template>
<FlexCol gap="20">
<Button
text="切换显示/隐藏(1秒动画)"
type="warning"
@click="showCustomDuration = !showCustomDuration"
/>
<SimpleTransition :show="showCustomDuration" :duration="1000">
<template #show="{ classNames }">
<view :class="['transition-box', ...classNames]">
慢动作过渡效果
</view>
</template>
</SimpleTransition>
</FlexCol>
</template>
<script setup>
import { ref } from 'vue';
import SimpleTransition from '@/components/anim/SimpleTransition.vue';
import Button from '@/components/basic/Button.vue';
import FlexCol from '@/components/layout/FlexCol.vue';
const showCustomDuration = ref(false);
</script>禁用动画
设置anim为false可以禁用过渡动画,元素将直接显示或隐藏。
vue
<template>
<FlexCol gap="20">
<Button
text="切换显示/隐藏(无动画)"
type="default"
@click="showDisabledAnim = !showDisabledAnim"
/>
<SimpleTransition :show="showDisabledAnim" :anim="false">
<template #show="{ classNames }">
<view :class="['transition-box', ...classNames]">
无动画效果
</view>
</template>
</SimpleTransition>
</FlexCol>
</template>
<script setup>
import { ref } from 'vue';
import SimpleTransition from '@/components/anim/SimpleTransition.vue';
import Button from '@/components/basic/Button.vue';
import FlexCol from '@/components/layout/FlexCol.vue';
const showDisabledAnim = ref(false);
</script>自定义过渡效果
通过自定义不同的CSS动画,可以实现各种过渡效果,如滑动、弹跳等。
vue
<template>
<FlexCol gap="20">
<Button
text="切换显示/隐藏(滑动)"
type="danger"
@click="showSlide = !showSlide"
/>
<SimpleTransition :show="showSlide" name="slide">
<template #show="{ classNames }">
<view :class="['transition-box', 'slide-box', ...classNames]">
滑动过渡效果
</view>
</template>
</SimpleTransition>
</FlexCol>
</template>
<script setup>
import { ref } from 'vue';
import SimpleTransition from '@/components/anim/SimpleTransition.vue';
import Button from '@/components/basic/Button.vue';
import FlexCol from '@/components/layout/FlexCol.vue';
const showSlide = ref(false);
</script>
<style scoped>
.slide-box {
width: 100%;
}
.slide-enter-active,
.slide-leave-active {
transition: transform 0.3s ease;
}
.slide-enter-from {
transform: translateX(-100%);
}
.slide-leave-to {
transform: translateX(100%);
}
</style>实际应用 - 模态框
SimpleTransition可以用于实现模态框等复杂组件的过渡效果。
vue
<template>
<FlexCol gap="20">
<Button
text="显示提示框"
type="success"
@click="showModal = !showModal"
/>
<SimpleTransition :show="showModal" name="modal">
<template #show="{ classNames }">
<view class="modal-overlay">
<view :class="['modal-content', ...classNames]">
<view class="modal-header">提示信息</view>
<view class="modal-body">
这是一个使用SimpleTransition实现的模态框过渡效果
</view>
<view class="modal-footer">
<Button
text="关闭"
type="default"
@click="showModal = false"
/>
</view>
</view>
</view>
</template>
</SimpleTransition>
</FlexCol>
</template>
<script setup>
import { ref } from 'vue';
import SimpleTransition from '@/components/anim/SimpleTransition.vue';
import Button from '@/components/basic/Button.vue';
import FlexCol from '@/components/layout/FlexCol.vue';
const showModal = ref(false);
</script>
<style scoped>
.modal-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 999;
}
.modal-content {
width: 80%;
max-width: 400px;
background-color: white;
border-radius: 12px;
overflow: hidden;
}
.modal-enter-active,
.modal-leave-active {
transition: all 0.3s ease;
}
.modal-enter-from,
.modal-leave-to {
opacity: 0;
transform: scale(0.9);
}
</style>API 参考
SimpleTransition
简单过渡组件。
Props
| 名称 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| show | 是否显示内容 | boolean | 是 | false |
| anim | 是否启用动画 | boolean | 否 | true |
| duration | 动画持续时间(毫秒) | number | 否 | 500 |
| name | 动画名称,用于生成CSS类名 | string | 否 | 'v' |
Slots
| 名称 | 说明 |
|---|---|
| show | 显示内容的插槽,接收classNames参数用于应用过渡样式 |
CSS 类名说明
组件会根据name属性生成以下CSS类名,您可以通过定义这些类名来实现自定义过渡效果:
{name}-enter-from: 进入过渡的开始状态{name}-enter-to: 进入过渡的结束状态{name}-enter-active: 进入过渡的活跃状态{name}-leave-from: 离开过渡的开始状态{name}-leave-to: 离开过渡的结束状态{name}-leave-active: 离开过渡的活跃状态