博客
关于我
微信小程序动画效果方法封装
阅读量:236 次
发布时间:2019-02-28

本文共 1680 字,大约阅读时间需要 5 分钟。

微信小程序的动画如何实现?归纳起来主要有以下几步:

首先在wxml中为需要添加动画的元素绑定动画属性,如下所示:

说明:animation与js里导出的动画名绑定 ,bindtap事件点击触发动画。

然后在js里编写生成动画的方法,主要分为创建动画、配置动画、导出动画,代码如下所示:

// 事件处理函数bindViewTap: function() {    //第一步:创建动画    var animation = wx.createAnimation({        // 动画持续时间        duration: 3000,        // 动画的方法,从开始到结束是一种什么样的规则        //'linear','ease','ease-in','ease-in-out','ease-out','step-start','step-end'        timingFunction: 'linear',        // 延迟时间        delay: 0,        // 运动原点        transformOrigin: '50%,50%,0',    })    // 第二步,配置动画    animation.rotate(360).scale(2).translate(10, -20).step();    animation.rotate(-360).scale(1).translate(0).step();    // animation.skew(45).step();    // 动画的矩阵变形    // animation.matrix(10,10,10,10,0,50).step();    // 第三步,导出动画    this.setData({        animationData: animation.export(),    })},

接下来,我们对这个动画方法进行简单封装。

// 第一个参数 that 是当前的页面对象// 第二个参数 param 是绑定页面动画名// 第三个参数 px 上下滑动的距离// 第四个参数 opacity 是需要修改为的透明度slideUpShow:function(that,param,px,opacity){    var animation = wx.createAnimation({        duration: 800,        timingFunction: 'ease',    });    animation.translateY(px).opacity(opacity).step()    // 将param转换为key    var json = '{"' + param + '":""}'    json = JSON.parse(json);    json[param] = animation.export()    // 设置动画    that.setData(json)}

通常对于这种全局的方法建议放在全局的app.js里,然后在其它页面通过getApp()获取全局方法。以下是调用实例:

然后是在当前页面里调用全局的动画方法,生成页面动画。

// pages/index/index.js// 获取小程序实例,也可以文件顶部直接引入onLoad: function (options) {    this.app = getApp();},// 页面显示时,触发动画onShow: function () {    this.app.slideUpShow(this, 'firstSlideUp', -200, 1)    setTimeout(function () {        this.app.slideUpShow(this, 'secondSlideUp', -200, 1)    }.bind(this), 200);}

 

转载地址:http://pasp.baihongyu.com/

你可能感兴趣的文章
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>