小程序封装加载动画

本文介绍了如何在小程序中封装全局加载动画,以提升用户体验。通过创建Loading组件,设置显示和隐藏方法,在App.js中加载并提供全局方法调用,实现在各个页面中统一管理和展示加载动画。在数据加载前后调用这些方法,可以在等待期间给用户一个反馈,缓解等待焦虑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

在小程序的开发中,页面的加载过程可能会因为网络状况的不好或数据量的过大而显得非常缓慢,这时候加上一个加载动画就能有效的缓解用户的等待焦虑感。而对于应用的多个页面来说,使用全局加载动画可以提高用户体验,让应用显得更加美观和专业。
本篇技术分享博客将为大家介绍在小程序中封装全局加载动画的具体实现步骤,帮助您提高小程序的用户体验。

效果

在这里插入图片描述

思路

封装全局加载动画的核心思路就是在每个页面加载数据时弹出加载动画,在数据加载完成后关闭动画。由于需要在所有页面中使用,我们需要将加载动画封装成一个全局组件,供所有页面调用。

具体实现步骤

第一步:创建Loading组件

我们新建一个Loading组件,用来展示动画,并在组件中实现开启和结束动画的方法。

wxml代码:

<view class='loading-mask'  wx:if="{{showLoading}}">
    <view class="container">
      <view class="box">
        <view class="atom"></view>
        <view class="atom"></view>
        <view class="atom"></view>
        <view class="dot"></view>
      </view>
    </view>
  </view>

js代码:

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 是否显示loading
    showLoading: {
      type: Boolean,
      value: false
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 开启动画
    show() {
      this.setData({showLoading: true});
    },
    // 关闭动画
    hide() {
      this.setData({showLoading: false});
    }
  }
});

//CSS样式

 container {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
}
.box {
	position: relative;
	width: 120rpx;
	height: 120rpx;
}
.dot{
	position: absolute;
	width: 10px;
	height: 10px;
	border-radius: 50%;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	background-color: #9eff03;
	animation: dotbreath 2s linear infinite;
}
.atom {
	position: absolute;
	width: 100%;
	height: 100%;
	border-radius: 50%;
	border-left-width: 6rpx;
	border-top-width: 6rpx;
	border-left-color: #20ff03;
	border-left-style: solid;
	border-top-style: solid;
	border-top-color: transparent;
	
}
.atom:nth-of-type(1) {
	left: 0%;
	top: 0%;
	animation: atom1 1s linear infinite;
}
.atom:nth-of-type(2) {
	right: 0%;
	top: 0%;
	animation: atom2 1s linear infinite;
}
.atom:nth-of-type(3) {
	right: 0%;
	bottom: 0%;
	animation: atom3 1s linear infinite;
}
@keyframes dotbreath {
	0% {
		opacity:1;
	}
	
	50%{
		opacity:0.5;
	}
	100%{
		opacity:1;
	}
}
@keyframes atom1 {
	0% {
		transform: rotateZ(120deg) rotateX(66deg) rotateZ(0deg);
	}
	100% {
		transform: rotateZ(120deg) rotateX(66deg) rotateZ(360deg);
	}
}
@keyframes atom2 {
	0% {
		transform: rotateZ(240deg) rotateX(66deg) rotateZ(0deg);
	}
	100% {
		transform: rotateZ(240deg) rotateX(66deg) rotateZ(360deg);
	}
}

@keyframes atom3 {
	0% {
		 transform: rotateZ(360deg) rotateX(66deg) rotateZ(0deg);
	}
	100% {
		 transform: rotateZ(360deg) rotateX(66deg) rotateZ(360deg);
	}
}
.loading-mask {
  height: 170rpx;
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.4);
  z-index: 100;
}
第二步:在app.js中加载Loading组件

在App.js中加载自定义组件Loading,并设定全局方法showLoading和hideLoading来显示和隐藏Loading组件。
js代码:

App({
  onLaunch: function () {
    // 加载Loading组件
    this.globalData.loading = this.selectComponent("#loading");
  },
  // 显示加载动画
  showLoading() {
    this.globalData.loading && this.globalData.loading.show();
  },
  // 隐藏加载动画
  hideLoading() {
    this.globalData.loading && this.globalData.loading.hide();
  }
});
第三步:在需要用到加载动画的地方调用全局方法

在需要用到加载动画的地方,通过调用App.js中的showLoading方法显示Loading动画,并在数据加载完毕后调用hideLoading方法关闭Loading动画。

js代码:

// 在页面中显示Loading动画
App.showLoading();

// 在数据请求成功回调中隐藏Loading动画
wx.request({
  url: 'url',
  success: function(res) {
    // 数据请求成功
    App.hideLoading();
  }
})

闭坑指南

在实际开发过程中,可能会遇到一些坑。我们需要注意以下几点:

  1. 组件命名
    在命名组件时要避免与已有的组件重名,以免命名冲突导致组件无法正常使用。
  2. 全局方法
    为确保全局方法生效,应在App.js中定义全局方法,并在需要调用的地方使用App.xxx()方法进行调用。
  3. 加载动画的选择
    在选择动画样式时,应根据产品需求和用户体验进行选择,以实现最好的效果,如果不喜欢这个Loading效果,可以在这里小程序加载动画找找其他的动画效果。

完整代码示例

完整代码示例如下:

//app.js
App({
  onLaunch: function () {
    // 加载Loading组件
    this.globalData.loading = this.selectComponent("#loading");
  },

  // 显示加载动画
  showLoading() {
    this.globalData.loading && this.globalData.loading.show();
  },

  // 隐藏加载动画
  hideLoading() {
    this.globalData.loading && this.globalData.loading.hide();
  }
});
 /**loading.wxml**/
<view class='loading-mask' wx:if="{{showLoading}}">
  <view class='loading-content'>
    <image class='loading-image' src='../../images/loading.gif'></image>
    <view class='loading-text'>加载中...</view>
  </view>
</view>
//loading.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 是否显示loading
    showLoading: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 开启动画
    show() {
      this.setData({showLoading: true});
    },
    // 关闭动画
    hide() {
      this.setData({showLoading: false});
    }
  }
});

页面部分

<view>
  <!--其他页面内容-->
  <Loading id="loading"></Loading>
</view>

js部分

// 在页面中显示Loading动画
App.showLoading();
// 在数据请求成功回调中隐藏Loading动画
wx.request({
  url: 'url',
  success: function(res) {
    // 数据请求成功
    App.hideLoading();
  }
})

样式部分

container {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
}
.box {
	position: relative;
	width: 120rpx;
	height: 120rpx;
}
.dot{
	position: absolute;
	width: 10px;
	height: 10px;
	border-radius: 50%;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	background-color: #9eff03;
	animation: dotbreath 2s linear infinite;
}
.atom {
	position: absolute;
	width: 100%;
	height: 100%;
	border-radius: 50%;
	border-left-width: 6rpx;
	border-top-width: 6rpx;
	border-left-color: #20ff03;
	border-left-style: solid;
	border-top-style: solid;
	border-top-color: transparent;
	
}
.atom:nth-of-type(1) {
	left: 0%;
	top: 0%;
	animation: atom1 1s linear infinite;
}
.atom:nth-of-type(2) {
	right: 0%;
	top: 0%;
	animation: atom2 1s linear infinite;
}
.atom:nth-of-type(3) {
	right: 0%;
	bottom: 0%;
	animation: atom3 1s linear infinite;
}
@keyframes dotbreath {
	0% {
		opacity:1;
	}
	
	50%{
		opacity:0.5;
	}
	100%{
		opacity:1;
	}
}
@keyframes atom1 {
	0% {
		transform: rotateZ(120deg) rotateX(66deg) rotateZ(0deg);
	}
	100% {
		transform: rotateZ(120deg) rotateX(66deg) rotateZ(360deg);
	}
}
@keyframes atom2 {
	0% {
		transform: rotateZ(240deg) rotateX(66deg) rotateZ(0deg);
	}
	100% {
		transform: rotateZ(240deg) rotateX(66deg) rotateZ(360deg);
	}
}

@keyframes atom3 {
	0% {
		 transform: rotateZ(360deg) rotateX(66deg) rotateZ(0deg);
	}
	100% {
		 transform: rotateZ(360deg) rotateX(66deg) rotateZ(360deg);
	}
}
.loading-mask {
  height: 170rpx;
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.4);
  z-index: 100;
}

总结

通过上述步骤,我们就完成了小程序中封装全局加载动画的具体实现方法。在实际开发中,我们可以根据实际需求对组件样式和方法进行调整和修改,以满足不同的开发需求。

### 小程序页面加载动画实现教程与示例 #### 加载动画的概念 加载动画是一种视觉反馈机制,用于提示用户当前操作正在进行中。它能够提升用户体验并减少等待焦虑感。 #### 微信小程序列表加载动画的实现思路 在微信小程序开发中,可以通过 `wx:if` 控制视图组件的显示状态来实现加载动画[^2]。具体来说,当数据尚未完全加载时,展示一个专门设计的加载动画;一旦数据加载完成,则隐藏该动画并渲染实际内容。 #### 完整代码示例 以下是基于微信小程序框架的一个简单加载动画实现案例: ```html <!-- WXML --> <view class="container"> <!-- 加载动画区域 --> <view wx:if="{{isLoading}}" class="loading-animation"> <image src="/images/loading.gif" mode="aspectFit"></image> <text>正在加载...</text> </view> <!-- 列表内容区域 --> <block wx:else> <scroll-view scroll-y style="height: 100vh;"> <view wx:for="{{items}}" wx:key="id" class="item">{{item.name}}</view> </scroll-view> </block> </view> ``` ```css /* WXSS */ .container { display: flex; justify-content: center; align-items: center; } .loading-animation { text-align: center; } .loading-animation image { width: 50px; height: 50px; } .item { padding: 10px; border-bottom: 1px solid #ccc; } ``` ```javascript // JS Page({ data: { isLoading: true, // 是否处于加载状态 items: [] // 数据列表 }, onLoad() { setTimeout(() => { // 模拟异步请求 this.setData({ isLoading: false, items: [ { id: 1, name: &#39;Item 1&#39; }, { id: 2, name: &#39;Item 2&#39; } ] }); }, 2000); // 延迟两秒模拟网络延迟 } }); ``` 上述代码展示了如何利用条件渲染 (`wx:if`) 和定时器模拟数据加载过程中的动画效果[^1]。 #### 自定义全局加载动画 如果希望在整个项目范围内统一管理加载动画,可以参考 Vue 的方式,通过封装工具函数或插件形式调用[^4]。例如,在小程序中也可以引入类似的逻辑: ```javascript const app = getApp(); function showLoading(text = &#39;加载中...&#39;) { wx.showToast({ title: text, icon: &#39;loading&#39;, duration: 10000, // 默认持续时间较长 mask: true // 防止点击穿透 }); } function hideLoading() { wx.hideToast(); } module.exports = { showLoading, hideLoading }; ``` 此方法允许开发者更方便地控制不同场景下的加载行为。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迪迦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值