We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Universal Methods

NOTE

Supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.

After a component is assigned the id attribute, you can use the ID to obtain the component objects and call methods on them.

animate

animate( keyframes: Keyframes, options: Options): void

A list of objects for setting animation styles and animation attributes.

Parameters

Expand
Name Type Mandatory Description
keyframes keyframes Yes Animation style.
options Options Yes Array of objects used to set animation attributes. For details, see Table 3 Options.

Table 1 keyframes

Expand
Attribute Type Description
frames Array<Style> Array of objects used to set animation styles. For details, see Table 2 Style.

Table 2 Style

Expand
Name Type Default Value Description
width number - Width set for the component during playback of the animation.
height number - Height set for the component during playback of the animation.
backgroundColor <color> none Background color set for the component during playback of the animation.
opacity number 1 Opacity set for the component. The value ranges from 0 to 1.
backgroundPosition string -

The value format is "x y", in percentage or px.

The first value indicates the horizontal position, and the second value indicates the vertical position.

If only one value is specified, the other value is 50%.

transformOrigin string 'center center'

Origin position of the transformed element.

The first value indicates the x-axis position. The value can be left, center, right, a length, or a percentage.

The second value indicates the y-axis position. The value can be top, center, bottom, a length, or a percentage.

transform Transform - Transformation type set for a transformed element.
offset number -

- The value of offset must be within (0.0, 1.0] and sorted in ascending order if it is provided.

- If there are only two frames, offset can be left empty.

- If there are more than two frames, offset is mandatory.

Table 3 Options

Expand
Name Type Default Value Description
duration number 0 Duration for playing the animation, in milliseconds. The valid value range is [0, +∞). If the value is less than 0, the default value is used.
easing string linear Time curve of the animation. For details about the supported types, see Table 4 Available values of the easing attribute.
delay number 0 Delay for the animation start. The default value indicates no delay.
iterations number | string 1 Number of times the animation will be played. number indicates a fixed number of playback operations, and Infinity indicates an unlimited number of playback operations.
direction6+ string normal

Mode of playing the animation.

normal: Plays the animation in forward loop mode.

reverse: Plays the animation in reverse loop mode.

alternate: Plays the animation in alternating loop mode. When the animation is played for an odd number of times, the playback is in forward direction. When the animation is played for an even number of times, the playback is in backward direction.

alternate-reverse: Plays the animation in reverse alternating loop mode. When the animation is played for an odd number of times, the playback is in reverse direction. When the animation is played for an even number of times, the playback is in forward direction.

fill string none

Start and end styles of the animation.

none: No style is applied to the target before or after the animation is executed.

forwards: The target keeps the state at the end of the animation (defined in the last key frame) after the animation is executed.

backwards6+: The animation uses the value defined in the first key frame during the animation-delay. When animation-direction is set to normal or alternate, the value in the from key frame is used. When animation-direction is set to reverse or alternate-reverse, the value in the to key frame is used.

both: The animation follows the forwards and backwards rules.

Table 4 Available values of the easing attribute

Expand
Value Description
linear The animation changes linearly.
ease-in The animation starts at a low speed and then picks up speed until the end. The cubic-bezier curve (0.42, 0.0, 1.0, 1.0) is used.
ease-out The animation ends at a low speed. The cubic-bezier curve (0.0, 0.0, 0.58, 1.0) is used.
ease-in-out The animation starts and ends at a low speed. The cubic-bezier curve (0.42, 0.0, 0.58, 1.0) is used.
friction The animation uses the friction cubic-bezier curve (0.2, 0.0, 0.2, 1.0).
extreme-deceleration The animation uses the extreme deceleration cubic-bezier curve (0.0, 0.0, 0.0, 1.0).
sharp The animation uses the sharp cubic-bezier curve (0.33, 0.0, 0.67, 1.0).
rhythm The animation uses the rhythm cubic-bezier curve (0.7, 0.0, 0.2, 1.0).
smooth The animation uses the smooth cubic-bezier curve (0.4, 0.0, 0.4, 1.0).
cubic-bezier(x1, y1, x2, y2) You can customize an animation speed curve in the cubic-bezier() function. The x and y values of each input parameter must be between 0 and 1.
steps(number, step-position)6+

The animation uses the step curve.

number must be set and only an integer is supported.

step-position is optional. It can be set to start or end. The default value is end.

Return value

The animation attributes are as follows.

Expand
Attribute Type Description
finished boolean Read-only attribute, which indicates whether the animation playback is complete. The value true indicates the animation playback is complete, and false indicates the opposite.
pending boolean Read-only attribute, which indicates whether the animation is waiting for the completion of other asynchronous operations (for example, start an animation with a delay). The value true indicates the animation is waiting, and false indicates the opposite.
playState string

Read-write attribute, which indicates the playback status of the animation:

- idle: The animation is not running (playback ended or not started).

- running: The animation is running.

- paused: The animation is paused.

- finished: The animation finishes playing.

startTime number Read-write attribute, which indicates the animation start time. This attribute is similar to delay in the Options attribute.

The animation methods are as follows.

Expand
Method Parameter Description
play - Plays the animation.
finish - Finishes the animation.
pause - Pauses the animation.
cancel - Cancels the animation.
reverse - Plays the animation in reverse.

The animation events are as follows.

Expand
Event Description
start6+ The animation starts.
cancel The animation is forcibly canceled.
finish The animation playback is complete.
repeat The animation repeats.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <div id="idName" class="box"></div>
  4. <div class="buttonBox">
  5. <button @click="start">
  6. start
  7. </button>
  8. <button @click="cancel">
  9. cancel
  10. </button>
  11. </div>
  12. </div>
Collapse
Word wrap
Dark theme
Copy code
  1. /* xxx.css */
  2. .container {
  3. flex-direction: column;
  4. justify-content: center;
  5. align-items: center;
  6. width: 100%;
  7. }
  8. .box{
  9. width: 200px;
  10. height: 200px;
  11. background-color: #ff0000;
  12. margin-top: 30px;
  13. }
  14. .buttonBox{
  15. margin-top: 30px;
  16. width: 250px;
  17. justify-content: space-between;
  18. }
  19. button{
  20. background-color: #8e8b89;
  21. color: white;
  22. width: 100px;
  23. height: 40px;
  24. font-size: 24px;
  25. }
Collapse
Word wrap
Dark theme
Copy code
  1. // xxx.js
  2. export default {
  3. data: {
  4. animation: '',
  5. options: {},
  6. frames: {}
  7. },
  8. onInit() {
  9. this.options = {
  10. duration: 1500,
  11. easing: 'friction',
  12. delay: 500,
  13. fill: 'forwards',
  14. iterations: 2,
  15. direction: 'normal',
  16. };
  17. this.frames = [
  18. {
  19. transform: {
  20. translate: '-120px -0px'
  21. }, opacity: 0.1, offset: 0.0
  22. },
  23. {
  24. transform: {
  25. translate: '120px 0px'
  26. }, opacity: 1.0, offset: 1.0
  27. }
  28. ];
  29. },
  30. start() {
  31. this.animation = this.$element('idName').animate(this.frames, this.options);
  32. this.animation.play();
  33. },
  34. cancel() {
  35. this.animation.cancel();
  36. }
  37. }

getBoundingClientRect

getBoundingClientRect(): <Rect>

Obtains the size of the element and its position relative to the window.

Return value

Table 5 Rect6+

Expand
Attribute Type Description
width number Width of the element.
height number Height of the element.
left number Offset between the left boundary of the element and the window.
top number Offset between the upper boundary of the element and the window.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // xxx.js
  2. var rect = this.$element('id').getBoundingClientRect();
  3. console.info(`current element position is ${rect.left}, ${rect.top}`);

createIntersectionObserver

createIntersectionObserver(param?: ObserverParam): Observer

Listens for the visibility of the element on the current page.

Parameters

Expand
Name Type Mandatory Description
param ObserverParam - Observer callback.

Table 6 ObserverParam6+

Expand
Attribute Type Description
ratios Array<number> Range for triggering the observer callback. If the component is not within the range, the observer callback is triggered.

Return value

Table 7 Methods supported by Observer6+

Expand
Method Parameter Description
observe callback: function Subscribes to events of the observed object. The callback method is called when the value is greater than or less than the threshold.
unobserve - Unsubscribes from events of the observed object.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // xxx.js
  2. let observer = this.$element('broad').createIntersectionObserver({
  3. ratios: [0.2, 0.5], // number
  4. });
  5. observer.observe((isVisible, ratio)=> {
  6. console.info('this element is ' + isVisible + 'ratio is ' + ratio)
  7. })
  8. observer.unobserve()