uniapp 种树界面

实现思路
在UniApp中实现种树界面,通常需要结合动画、进度条和交互逻辑。核心要素包括:
- 树苗成长动画:通过CSS动画或Lottie实现动态效果。
- 进度显示:使用进度条组件展示成长进度。
- 交互按钮:浇水、施肥等操作触发成长逻辑。
关键代码示例
模板部分
<template>
<view class="tree-container">
<!-- 树苗动画区域 -->
<view class="tree" :style="{ transform: `scale(${growthProgress})` }">
<image src="/static/tree-seedling.png" mode="aspectFit"></image>
</view>
<!-- 进度条 -->
<progress :percent="growthPercent" stroke-width="10" activeColor="#07C160" />
<!-- 操作按钮 -->
<view class="actions">
<button @click="waterTree">浇水</button>
<button @click="fertilizeTree">施肥</button>
</view>
</view>
</template>
脚本部分
<script>
export default {
data() {
return {
growthPercent: 0, // 进度百分比
growthProgress: 0.5 // 初始缩放比例
};
},
methods: {
waterTree() {
this.growthPercent = Math.min(100, this.growthPercent + 10);
this.updateTreeSize();
},
fertilizeTree() {
this.growthPercent = Math.min(100, this.growthPercent + 20);
this.updateTreeSize();
},
updateTreeSize() {
// 根据进度调整树的大小(0.5 ~ 1.5倍)
this.growthProgress = 0.5 + (this.growthPercent / 100);
}
}
};
</script>
样式部分
<style>
.tree-container {
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}
.tree {
margin: 30px 0;
transition: transform 0.5s ease;
}
.tree image {
width: 150px;
height: 200px;
}
.actions {
margin-top: 20px;
}
button {
margin: 10px;
}
</style>
进阶优化建议
- 动画效果:使用Lottie或GIF实现更复杂的树苗生长动画。
- 数据持久化:通过
uni.setStorage保存进度,避免用户退出后重置。 - 音效增强:在浇水/施肥时播放音效(
uni.createInnerAudioContext)。 - 多阶段成长:根据进度切换不同树苗图片(如幼苗、成树、开花等)。
示例资源
- 免费素材:在Pixabay搜索“tree seedling”获取透明PNG素材。
- Lottie动画:使用LottieFiles的植物生长动画。
通过上述代码和思路,可快速构建一个基础的种树界面,并根据需求扩展功能。







