实现类似腾讯元宝小程序的页面回退动画效果
要在uniapp微信小程序中实现类似腾讯元宝小程序的页面回退效果(点击回退按钮后页面缩放变小并移到右边,左边出现菜单等信息),你可以按照以下步骤实现:
方案一:使用CSS动画和页面遮罩
- 修改页面结构:
<template>
<view class="container">
<!-- 主内容区域 -->
<view class="main-content" :class="{ 'shrinked': isShrinked }">
<!-- 你的页面内容 -->
<view class="header">
<view class="back-btn" @click="toggleShrink">
<text>返回</text>
</view>
</view>
<!-- 页面主体内容 -->
</view>
<!-- 左侧菜单 (默认隐藏) -->
<view class="side-menu" :class="{ 'show': isShrinked }">
<!-- 菜单内容 -->
</view>
<!-- 遮罩层 (点击可恢复) -->
<view class="mask"
:class="{ 'show': isShrinked }"
@click="toggleShrink"
v-if="isShrinked"></view>
</view>
</template>
- 添加样式:
.container {
position: relative;
width: 100%;
height: 100vh;
overflow: hidden;
}
.main-content {
position: absolute;
width: 100%;
height: 100%;
background: #fff;
transition: all 0.3s ease;
transform-origin: right center;
z-index: 10;
}
.main-content.shrinked {
transform: scale(0.8) translateX(-30%);
box-shadow: -5px 0 15px rgba(0, 0, 0, 0.1);
border-radius: 12px;
}
.side-menu {
position: absolute;
left: -70%;
top: 0;
width: 70%;
height: 100%;
background: #f7f7f7;
transition: all 0.3s ease;
z-index: 5;
}
.side-menu.show {
left: 0;
}
.mask {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.3);
z-index: 8;
opacity: 0;
transition: opacity 0.3s ease;
}
.mask.show {
opacity: 1;
}
- 添加交互逻辑:
<script>
export default {
data() {
return {
isShrinked: false
}
},
methods: {
toggleShrink() {
this.isShrinked = !this.isShrinked
}
}
}
</script>
方案二:使用微信小程序原生动画API
如果你需要更精细的动画控制,可以使用微信小程序的动画API:
methods: {
toggleShrink() {
if (this.isShrinked) {
// 恢复动画
const animation = uni.createAnimation({
duration: 300,
timingFunction: 'ease'
})
animation.scale(1).translateX(0).step()
this.animationData = animation.export()
// 菜单隐藏动画
const menuAnimation = uni.createAnimation({
duration: 300,
timingFunction: 'ease'
})
menuAnimation.left('-70%').step()
this.menuAnimationData = menuAnimation.export()
} else {
// 缩放动画
const animation = uni.createAnimation({
duration: 300,
timingFunction: 'ease'
})
animation.scale(0.8).translateX('-30%').step()
this.animationData = animation.export()
// 菜单显示动画
const menuAnimation = uni.createAnimation({
duration: 300,
timingFunction: 'ease'
})
menuAnimation.left('0').step()
this.menuAnimationData = menuAnimation.export()
}
this.isShrinked = !this.isShrinked
}
}
注意事项
- 性能优化:动画可能会影响性能,特别是在低端设备上,建议合理使用动画时长和复杂度。
- 兼容性:不同小程序平台对CSS属性的支持可能不同,需要进行测试。
- 用户体验:确保有明确的交互方式让用户知道如何恢复页面(如点击遮罩层或菜单外的区域)。
- 状态管理:如果需要在多个页面使用此效果,可以考虑使用Vuex或全局状态管理。
- 自定义导航栏:如果你使用了自定义导航栏,需要确保返回按钮的位置和样式与设计一致。
以上方案可以根据你的具体需求进行调整,比如修改缩放比例、动画时长、阴影效果等参数来达到最佳视觉效果。