# animation

# Common Ways of Interface Animation

In Mini programs, you can usually use CSS gradual change and CSS animation To create simple interface animations.

{% Minicode ('oHKxDPm47h5k') %}

Animation process, you can use the Binding transition end bindanimationstart bindanimationiteration Binding animation end To listen for animated events.

Name of event meaning
transitionend CSS End of Gradient or wx.createAnimation Ending a Phase
animationstart CSS Animation begins
animationiteration CSS Animation ends a phase
End of animation CSS End of animation

Note: These events are not bubbling events and need to be bound to the node where the animation actually happened.

At the same time, you can also use wx.createAnimation Interface to dynamically create simple animation effects. (The following keyframe animation interface is recommended in the new version of the Mini Program Basics library instead.))

# Keyframe animation

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

Mini Program base library 2.9.0 Began to support a more user-friendly way of creating animations in place 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 a keyframe, you can use this.animate Interface:

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

Dxplaination of parameters

attribute type Default value Required Introductions
selector String yes Selector (same as SelectorQuery.select Of the selector format)
keyframes Array yes Key frame information
duration Number yes Animation duration in milliseconds
callback function no The callback function after the animation is complete

keyframes The structure of the object in

attribute type Default value Required Introductions
offset Number no Offset of key frame, range[0-1]
ease String linear no Animation slow function
TransformOrigin String no Base point position, i.e. CSS Transform-origin
backgroundColor String no The background color, i.e. CSS background-color
bottom Number/String no Bottom edge position, i.e. CSS bottom
height Number/String no Height, i.e. CSS height
left Number/String no Position on the left, i.e. CSS left
width Number/String no Width, i.e. CSS width
opacity Number no Opacity, i.e. CSS opacity
right Number no Position on the right, i.e. CSS right
top Number/String no Top edge position, i.e. CSS top
matrix Array no Transformation matrix, i.e. CSS transform matrix
matrix3d Array no Three-dimensional transformation matrix, i.e. CSS transform matrix3d
Rotate Number no Rotation, i.e. CSS transform Rotate
rotate3d Array no Three-dimensional rotation, i.e. CSS transform rotate3d
rotateX Number no X Direction of rotation, i.e. CSS transform rotateX
rotateY Number no Y Direction of rotation, i.e. CSS transform rotateY
rotateZ Number no Z Direction of rotation, i.e. CSS transform rotateZ
scale Array no Scaling, i.e. CSS transform scale
scale3d Array no Three-dimensional scaling, i.e. CSS transform scale3d
scaleX Number no X Directional scaling, i.e. CSS transform scaleX
scaleY Number no Y Directional scaling, i.e. CSS transform scaleY
scaleZ Number no Z Directional scaling, i.e. CSS transform scaleZ
skew Array no Tilt, i.e. CSS transform skew
skewX Number no X Tilt in the direction, i.e. CSS transform skewX
skewY Number no Y Tilt in the direction, i.e. CSS transform skewY
translate Array no Displacement, i.e. CSS transform translate
translate3d Array no Three-dimensional displacement, i.e. CSS transform translate3d
translateX Number no X Direction displacement, i.e. CSS transform translateX
translateY Number no Y Direction displacement, i.e. CSS transform translateY
translateZ Number no Z Direction displacement, i.e. CSS transform translateZ

# sample code

{% Minicode ('P73kJ7mi7UcA' ) %}


  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(" Purged#Opacity and rotate properties on container ")
      })
  }.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("Cleared all animation properties on.block")
    })
  }.bind (this))

call animate API After the node will add some style properties to override the original corresponding style. If you need to clear these styles, you can use them after all animations on the node have been executed this.clearAnimation Clear these properties.

this.clearAnimation(selector, options, callback)

Dxplaination of parameters

attribute type Default value Required Introductions
selector String yes Selector (same as SelectorQuery.select Of the selector format)
options Object no Properties that need to be cleared. If you do not fill in, it is all cleared
callback Function no Clear the callback function after completion

# Scroll Driven Animation

We found that changing the progress of the animation based on the scroll position is a more common scenario, and this kind of animation can make the interface interaction feel more coherent and natural. Therefore, the Mini Program base library 2.9.0 Started to support a scrolldriven animation mechanism.

Based on the above key frame animation interface, add a ScrollTimeline Is used to bind scrolling elements (currently only supported scroll-view)。The interface is defined as follows:

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

ScrollTimeline The structure of the object in

attribute type Default value Required Introductions
scrollSource String yes A selector that specifies a scrolling element (only supports Scroll-view), which drives the progress of the animation as it scrolls
orientation String vertical no Specifies the direction of scrolling. The valid value is horizontal or vertical
startScrollOffset Number yes Specifies the rolling offset at which the drive animation progress begins, in units px
endScrollOffset Number yes Specifies the rolling offset that stops the progress of the drive animation, in units px
Timerange Number yes The length of time for the start and end scrolling range mapping, which can be used with the time in the keyframe animation (duration) Match, Unit ms

# sample code

{% Minicode ('994o8jmY7FcQ') %}

  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 methods

In some complex scenes, the above animation methods may not be applicable.

WXS Responding to Events In a way that can be achieved by using the WXS To respond to events by means of dynamically adjusting the node's style Property. Through constant change style The value of the property can be animated. At the same time, this method can also dynamically generate animations based on user touch events.

Continuous use setData To change the interface can also achieve the effect of animation. This can change the interface arbitrarily, but usually causes a large delay or stuttering, and even causes the Mini Program to freeze. At this point you can do this by putting the page's setData to change into Custom Components to hit the target setData To improve performance. The following example is using the setData To animate a stopwatch.

{% Minicode ('cRTvdPmO7d5T') %}