# animation

# Common ways of animating interfaces

In Weixin Mini Program, you can often use CSS gradients and CSS animations to create simple interface animations.

Preview with Developer Tool

During animation, you can usebindtransitionend``bindanimationstart``bindanimationiteration``bindanimationendCome listen to the animated events.

Name of event meaning
transitionend CSS Gradient End or wx.createAnimation End a phase
animationstart CSS Animation Begins
animationiteration CSS animation ends a phase
animationend End of CSS Animation

Note: None of these events are bubble events and need to be bound to the node where the animation actually occurred before they can take effect.

You can also use the wx.createAnimation interface to dynamically create simple animation effects.(In the new version of the Weixin Mini Program base library, it is recommended to use the following key frame animation interface instead.))

# Key frame animation

Start from base library version 2.9.0. Please remaining backward compatible.

Starting with the Weixin Mini Program base library {% version (2.9.0)%} supports a more user-friendly way of creating animations instead of the old wx.createAnimation 。It has better performance and a more controllable interface.

In a page or custom component, when you need to animate keyframes, you can use thethis.animateinterface:

this.animate(selector, keyframes, duration, callback)

Parameter explaination

attribute type Default values Required to fill in Introductions
selector String yes Selector (same as SelectorQuery.select selector format)
keyframes Array yes Critical frame information
duration Number yes Animation duration in milliseconds
callback function no callback function when the animation is complete

Structure of objects in keyframes

attribute type Default values Required to fill in Introductions
offset Number no Shifts of key frames, range [0-1]
ease String linear no Animation slowing function
transformOrigin String no The base point position, i.e. the CSS transform-origin
backgroundColor String no CSS background-color
opacity Number no Opacity, or CSS opacity
width String no Width, CSS width
height String no Height, CSS height
left String no Left position, CSS left
top String no The top edge position, the CSS top
right String no Right position, CSS right
bottom String no Bottom position, CSS bottom
matrix Array no Transform matrix, CSS transform matrix
matrix3d Array no 3D transformation matrix, i.e. CSS transform matrix3d
rotate Number no CSS transform rotate
rotate3d Array no 3D rotation, i.e. CSS transform rotate3d
rotateX Number no CSS transform rotateX
rotateY Number no Y rotation, CSS transform rotateY
rotateZ Number no CSS transform rotateZ
scale Array no CSS transform scale
scale3d Array no 3D scaling, i.e. CSS transform scale3d
scaleX Number no X-oriented scaling, i.e. CSS transform scaleX
scaleY Number no Y direction scaling, i.e. CSS transform scaleY
scaleZ Number no Z direction scaling, i.e. CSS transform scaleZ
skew Array no CSS transform skew
skewX Number no CSS transform skewX
skewY Number no Y skewy, CSS transform skewY
translate Array no The CSS transform translate
translate3d Array no CSS transform translate3d
translateX Number no X 方向位移,即 CSS transform translateX
translateY Number no Y 方向位移,即 CSS transform translateY
translateZ Number no Z 方向位移,即 CSS transform translateZ

# sample code

Preview with Developer Tool


  this.animate('#container', [
    { opacity: 1.0, rotate: 0, backgroundColor: '#FF0000' },
    { opacity: 0.5, rotate: 45, backgroundColor: '#00FF00'},
    { opacity: 0.0, rotate: 90, backgroundColor: '#FF0000' },
    ], 5000, function () {
      this.clearAnimation('#container', { opacity: true, rotate: true }, function () {
        console.log("清除了#container上的opacity和rotate属性")
      })
  }.bind(this))

  this.animate('.block', [
    { scale: [1, 1], rotate: 0, ease: 'ease-out'  },
    { scale: [1.5, 1.5], rotate: 45, ease: 'ease-in', offset: 0.9},
    { scale: [2, 2], rotate: 90 },
  ], 5000, function () {
    this.clearAnimation('.block', function () {
      console.log("清除了.block上的所有动画属性")
    })
  }.bind(this))

Calling the animate API will add some style attributes to the node to override the original corresponding style.If you need to clear these styles, you can clear them usingthis.clearAnimationafter all the animation on that node has been executed.

this.clearAnimation(selector, options, callback)

Parameter explaination

attribute type Default values Required to fill in Introductions
selector String yes Selector (same as SelectorQuery.select selector format)
options Object no Properties that need to be erased, all erased without filling in
callback Function no When clearing a callback function is complete

# Scroll-driven animation

We found that constantly changing the animation's pace based on the scrolling position is a common scenario, which can make the interface interaction feel coherent and natural, and the experience is better.Therefore, starting with the Weixin Mini Program base library {% version (2.9.0)%}, a scroll-driven animation mechanism is supported.

Based on the above keyframe animation interface, a newScrollTimelineparameter is added to bind scrolling elements (currently only supported scroll-view).The interface is defined as follows:

this.animate(selector, keyframes, duration, ScrollTimeline)

Structure of objects in ScrollTimeline

attribute type Default values Required to fill in Introductions
scrollSource String yes Specifies a selector for a scrolling element (scroll-view only) that drives the progress of the animation as it scrolls
orientation String vertical no Specify the direction of the scrolling. The potency is horizontal or vertical
startScrollOffset Number yes Specifies the scroll offset, in px, to start driving animation progress
endScrollOffset Number yes Specifies the scroll offset, in px, that stops driving animation progress
timeRange Number yes Length of time for the start and end scroll range mapping, which can be used to match the duration in the keyframe animation, in ms

# sample code

Preview with Developer Tool

  this.animate('.avatar', [{
    borderRadius: '0',
    borderColor: 'red',
    transform: 'scale(1) translateY(-20px)',
    offset: 0,
  }, {
    borderRadius: '25%',
    borderColor: 'blue',
    transform: 'scale(.65) translateY(-20px)',
    offset: .5,
  }, {
    borderRadius: '50%',
    borderColor: 'blue',
    transform: `scale(.3) translateY(-20px)`,
    offset: 1
  }], 2000, {
    scrollSource: '#scroller',
    timeRange: 2000,
    startScrollOffset: 0,
    endScrollOffset: 85,
  })

  this.animate('.search_input', [{
    opacity: '0',
    width: '0%',
  }, {
    opacity: '1',
    width: '100%',
  }], 1000, {
    scrollSource: '#scroller',
    timeRange: 1000,
    startScrollOffset: 120,
    endScrollOffset: 252
  })

# Advanced ways of animation

In some complex scenarios, the above animation methods may not be appropriate.

The way WXS responds to events You can dynamically adjust the style attribute of a node by using WXS to respond to events.Animation can be achieved by changing the value of the style attribute over and over again. At the same time, this method can also dynamically generate animations based on the user's touch events.

Continuous use of setData to change the interface method can also achieve the effect of animation.This can change the interface arbitrarily, but usually results in large latency or stuttering, and even Weixin Mini Program dead.You can improve performance by changing the page's setData to the setData in the custom component .The following example is an example of using setData to implement stopwatch animation.

Preview with Developer Tool