Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program animation


May 17, 2021 WeChat Mini Program Development Document


Table of contents


Animation

A common way to animate an interface

In small programs, you can often use CSS gradients and CSS animations to create simple interface animations.

During the animation process, you can use bindtransitionend bindanimationstart bindanimationation bindanimationend to listen for animation events.

The name of the event Meaning
transitionend The CSS gradient ends or wx.createAnimation ends a phase
animationstart The CSS animation begins
animationiteration The CSS animation ends a phase
animationend The CSS animation ends

Note: None of these events are bubbling events and need to be bound to the node where the animation actually occurred for them to take effect.

You can also use the wx.createAnimation interface to dynamically create simple animations. ( The key frame animation interface described below is recommended in the new version of the little program foundation library instead.)

Key frame animation

Base library 2.9.0 starts to support, and low versions need to be compatible.

Starting with the small program base library 2.9.0, a friendlier animation creation method is supported to replace the old wx.createAnimation. It has better performance and a more controlled interface.

In a page or custom component, you can use the this.animate interface when keyframe animation is required:

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

Description of the parameters

Property Type The default Required Description
selector String Is Selector (with SelectorQuery.selector format)
keyframes Array Is Keyframe information
duration Number Yes Animation lasts long (milliseconds)
callback function no The callback function after the completion of the animation

The structure of the object in Keyframes

Attributes type Defaults Required illustrate
offset Number no Offset, range of key frames [0-1]
ease String linear no Animation slower function
transformOrigin String no CSS Transform-Origin
backgroundColor String no Background color, CSS Background-Color
bottom Number/String no Caside position, CSS Bottom
height Number/String no Height, CSS Height
left Number/String no Left position, CSS LEFT
width Number/String no Width, CSS Width
opacity Number no Opacity, CSS Opacity
right Number no Right position, ie CSS Right
top Number/String Whether Top edge position, which is CSS top
matrix Array Whether Transform matrix, or CSS transform matrix
matrix3d Array Whether A 3D transformation matrix, or CSS transform matrix3d
rotate Number Whether Rotation, i.e. CSS transform rotate
rotate3d Array Whether 3D rotation, i.e. CSS transform rotate3d
rotateX Number Whether The X direction rotates, i.e. CSS transform rotateX
rotateY Number Whether Y direction rotation, i.e. CSS transform rotateY
rotateZ Number Whether The Z direction rotates, i.e. CSS transform rotateZ
scale Array Whether Zoom, that is, CSS transform scale
scale3d Array Whether 3D scaling, i.e. CSS transform scale3d
scaleX Number Whether X-directional scaling, or CSS transform scaleX
scaleY Number Whether Y-directional scaling, i.e. CSS transform scaleY
scaleZ Number Whether Z-directional scaling, i.e. CSS transform scaleZ
skew Array Whether Tilt, that is, CSS transform skew
skewX Number Whether The X direction tilts, i.e. CSS transform skewX
skewY Number Whether Y direction tilt, that is, CSS transform skewY
translate Array Whether Displacement, or CSS transform translate
translate3d Array Whether 3D displacement, or CSS transform translate3d
translateX Number Whether X-directional displacement, or CSS transform translateX
translateY Number Whether Y direction displacement, that is, CSS transform translateY
translateZ Number Whether Z-direction displacement, or CSS transform translateZ

The sample code

Preview the effect in the 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))

When the animate API is called, some new style properties are added to the node to override the original corresponding style. If you need to clear these styles, you can use this.clearAnimation to clear these properties after all the animations on the node have been executed.

this.clearAnimation(selector, options, callback)

Description of the parameters

Property Type The default Required Description
selector String Is Selector (with SelectorQuery.selector format)
options Object Whether Properties that need to be cleared are cleared if they are not filled in
callback Function Whether The callback function after the cleanup is complete

Scroll-driven animation

We found that constantly changing the progress of animations based on scrolling positions is a more common scenario, and this type of animation gives the feeling that the interface interaction is consistent and natural, and the experience is better. Therefore, a scroll-driven animation mechanism is supported starting with the small program base library 2.9.0.

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

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

The structure of the object in ScrollTimeline

Property Type The default Required Description
scrollSource String Is Specifies the selector for the scroll element (scroll-view only), which drives the progress of the animation as it scrolls
orientation String vertical Whether Specify the direction of scrolling. The valid value is horizontal or vertical
startScrollOffset Number Is Specifies the rolling offset, in px, that starts driving the progress of the animation
endScrollOffset Number Is Specifies the rolling offset, in px, that stops driving the progress of the animation
timeRange Number Is The length of time that the scroll range map starts and ends, which can be used to match the time (duration) in the key frame animation, in ms

The sample code

Preview the effect in the 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 animation

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

WXS responds to events by dynamically adjusting the node's style property by using WXS to respond to events. Y ou can animate by constantly changing the value of the style property. It also dynamically generates animations based on the user's touch events.

Continuous use of setData to change the interface can also achieve the effect of animation. T his can change the interface arbitrarily, but usually results in large delays or catatons, or even a small program deadlock. Y ou can improve performance by changing the setData of the page to setData in a custom component. The following example is an example of using setData to implement stopwatch animation.