微信小程序动画:高度渐变,left渐变
今天在测试微信小程序动画的时候遇到了坑,需求是这样的点击时子元素从外部滑动回来,父元素的高度跟随子元素的高度改变。

实现子元素left为0并不复杂,但是改变父元素box的高度的时候却遇到了坑,因为是需要跟随子元素right的高度来改变父元素box的高度的,并且子元素right的高度不确定,我们需要先获取子元素的高度。
在改变高度的时候出错了,高度未改变。在测试时发现
var box = wx.createAnimation(option); // 创建动画
var obj = wx.createSelectorQuery();
obj.select('.anr').boundingClientRect(function (rect) {//获取子元素高度
box.height(rect.height).step();//改变父元素高度
console.log(1);
}).exec();
console.log(2);
that.setData({
box: box.export()
});

先打印的竟然是2,原来是一个异步操作,这就可以理解为什么执行无效了。改成这样
obj.select('.anr').boundingClientRect(function (rect) {//获取子元素高度
var box = wx.createAnimation(option); // 创建动画
box.height(rect.height).step();//改变父元素高度
that.setData({
box: box.export()
});
}).exec();
想着应该没问题了,但是遇到了另外一个坑,父元素的高度一下子变成了预设的效果,并没有Animation的渐变效果。本身父元素的高度是由left这个子元素撑起来的,给父元素预设一个高度这个问题就解决了。渐变效果就实现了。
源码解析
wxml
<view class="box" animation="{{box}}">
<view class="anl">left</view>
<view class="anr" animation="{{anr}}">right</view>
</view>
<button bindtap="add" wx:if="{{down}}">goDown</button>
<button bindtap="goback" wx:else>goBack</button>
wxss
/* pages/userinfo/index.wxss */
.box{
position: relative;
height: 200rpx;
overflow: hidden;
text-align: center;
color: white;
font-size: 120rpx;
}
.anl{
height: 200rpx;
background-color: red;
}
.anr{
background-color: green;
height: 400rpx;
width: 100%;
position: absolute;
left: 100%;
top: 0;
}
.add{
background-color: yellow;
height: 100rpx;
}
js
// pages/userinfo/index.js
Page({ /**
* 页面的初始数据
*/
data: {
box: {},
anr: {},
down:true
},
add: function () {
this.setData({
down: false
});
var that = this;
let option = {
duration: 1000, // 动画执行时间
timingFunction: 'ease-in' // 动画执行效果
};
var anr = wx.createAnimation(option);// 创建动画
this.anr=anr;
anr.left(0).step();
that.setData({
anr: anr.export()
});
var obj = wx.createSelectorQuery();
obj.select('.anr').boundingClientRect(function (rect) {//获取子元素高度
var box = wx.createAnimation(option); // 创建动画
that.box=box;
box.height(rect.height).step();//改变父元素高度
that.setData({
box: box.export()
});
}).exec();
},
goback:function(){
this.setData({
down:true
});
this.box.height('200rpx').step();
this.setData({
box:this.box.export()
});
this.anr.left('750rpx').step();
this.setData({
anr:this.anr.export()
})
}
})
微信小程序动画:高度渐变,left渐变的更多相关文章
- 微信小程序动画之圆形进度条
微信小程序动画之圆形进度条 上图: js: //获取应用实例 var app = getApp() var interval; var varName; var ctx = wx.createCanv ...
- 精确计算微信小程序scrollview高度,全机型适配
众所周知,可以滑动的 scroll 组件在移动端非常的重要,几乎每个页面都要用到. 而小程序的 scroll-view 组件就比较坑了,非得指定一个高度才能正常使用.布局复杂的时候谁还给你算高度啊.. ...
- 微信小程序动画实现(API,css)
微信小程序动画API实现 index.js clicktap:function(){ var Animation=wx.createAnimation({ duration: 2000, }) Ani ...
- 微信小程序动画技巧
用微信小程序自带的wx.createAnimation api可创建动画,该动画效果相比css写的动画更流畅. 栗子与用法,见官网:https://mp.weixin.qq.com/debug/wxa ...
- 微信小程序动画之弹出菜单
用微信小程序做了一个动画,效果如上图: 代码: js: Page({ data: { isPopping: false, animPlus: {}, animCollect: {}, animTran ...
- 如何实现微信小程序动画?添加到我的小程序动画实现详细讲解,轻松学会动画开发!附壁纸小程序源码下载链接
为了让用户能尽可能多地使用小程序,也算是沉淀用户,现在很多小程序中,都有引导用户"添加到我的小程序"的操作提示,而且大多都是有动画效果.在高清壁纸推荐小程序首页,用户每次进入,都会 ...
- 微信小程序swiper高度自适应,swiper的子元素高度不固定
小程序 swiper 组件默认高度150px,并且如果子元素过高,swiper不会自适应高度 解决方案一: (总体来说不够完美,适合满屏滑动) 如果不是满屏的状态,用scroll-view IOS滑动 ...
- 微信小程序 --- 动画
动画的基本使用: 旋转动画 缩放动画 偏移动画 倾斜动画 矩阵动画 动画API:wx.createAnimation(object) 示例:创建一个点击的动画 <view class=" ...
- 微信小程序——动画操作时,rpx 和 px 的转换计算。
嫌长版本: var rpx = 10000; var systemInfo = wx.getSystemInfoSync(); var px = rpx / 750 * systemInfo.wind ...
随机推荐
- Blend Visual studio 和Visual studio 的区别
blend for visual studio 这个并不是用于开发中写代码的,而是专门用来做WPF的界面设计的.这是为了让专业的人做专业的事,尽量让写代码的人只使用visual studio,而做界面 ...
- qspi nor
qspi: sf probe SF: Detected s25fl256s_256k with page size 512 Bytes, erase size 256 KiB, total 32 Mi ...
- Oracle之:Function :strFormatDate()
create or replace function strFormatDate(i_datestr in varchar2) return date is begin if i_datestr is ...
- 【Winfrom-Panel】Panel隐藏与显示,自动隐藏菜单, Auto-Hide Menu
设计界面:2个button,一个panel 当鼠标移动到btnHome时,显示menuPanel 当鼠标离开btnHome时,隐藏menuPanel 当btnPin箭头向左时,menuPanel为自动 ...
- 【leetcode】1281. Subtract the Product and Sum of Digits of an Integer
题目如下: Given an integer number n, return the difference between the product of its digits and the sum ...
- kubernetes的搭建以及dashboard页面的启动
###查看kubernetes状态 ``` kubectl get pods -A #查看相关状态 kubectl get cs #查看k8s的ready状态 kubectl get node #查看 ...
- 原创:实现atoi函数
#include <stdio.h> #include <stdlib.h> #include <limits.h> int my_atoi(char *str) ...
- Java当中的集合框架
Java当中的集合框架 01 在我们班里有50位同学,就有50位对象. // 简书作者:达叔小生 Student[] stus = new Student[20]; 结果来了一位插班生,该同学因为觉得 ...
- 两个线程,一个线程打印1~52,另一个线程打印字母A-Z,打印顺序为12A34B56C……5152Z
使用wait,notify实现 public class Test { public synchronized void a() { for (int i = 1; i <= 52; i++) ...
- jQuery文档操作之克隆操作
语法: $(selector).clone(); 解释:克隆匹配的DOM元素 $("button").click(function(event) { //1.clone():克隆匹 ...