riku
2026-01-21 a2c5def7cf9562312a54216bc195a6ea071a26c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
 * z轴堆叠列表展开收缩动画
 * 堆叠项类名为 `${this.selector}-${index}`,列表容器类名为`${this.selector}-wrap`
 * @param {*} options
 */
function ZTranslate(options) {
  let { content, selector, size, margin } = options;
  this.content = content;
  this.selector = selector;
  this.size = size;
  this.margin = margin;
  this.toggleList = [];
  this.expanded = false;
 
  this.init();
}
 
ZTranslate.prototype = {
  init() {
    this.keyframes = [];
    let len = 0;
    for (let i = 0; i < this.size; i++) {
      let keyframe = [];
      keyframe.push({
        translateY: '0px',
        top: i * 3 + 'px',
        left: i * 2 + 'px',
        right: 0 - i * 2 + 'px',
        ease: 'ease',
      });
      this._getEndY(i, res => {
        len += res + this.margin * (i == 0 ? 0 : 1);
        let translateY = len - res + 'px';
        keyframe.push({
          translateY: translateY,
          top: '0px',
          left: '0px',
          right: '0px',
          ease: 'ease',
        });
 
        if (i == this.size - 1) {
          this.wrapKeyframe = [
            { height: '0px', ease: 'ease' },
            { height: len + 'px', ease: 'ease' },
          ];
        }
      });
      this.keyframes.push(keyframe);
    }
  },
 
  expand(callback) {
    if (this.toggleList.length < this.size) return;
    this.content.animate(`${this.selector}-wrap`, this.wrapKeyframe, 200);
    let length = this.keyframes.length;
    for (let i = 0; i < this.keyframes.length; i++) {
      const k = this.keyframes[i];
      this.content.animate(
        `${this.selector}-${i}`,
        k,
        200,
        function () {
          if (i == length - 1 && typeof callback === 'function') {
            callback();
          }
        }.bind(this.content),
      );
    }
    this.expanded = true;
  },
 
  collapse(callback) {
    if (this.toggleList.length < this.size) return;
    this.content.animate(
      `${this.selector}-wrap`,
      [...this.wrapKeyframe].reverse(),
      200,
    );
    let length = this.keyframes.length;
    for (let i = 0; i < this.keyframes.length; i++) {
      const k = this.keyframes[i];
      this.content.animate(
        `${this.selector}-${i}`,
        [...k].reverse(),
        200,
        function () {
          if (i == length - 1 && typeof callback === 'function') {
            callback();
          }
        }.bind(this.content),
      );
    }
    this.expanded = false;
  },
 
  slideOut(i, callback) {
    const selector = `${this.selector}-${i}`;
    const keyframe = this._getCurrentKeyframe(i);
    this.content.animate(
      selector,
      [
        { ...keyframe, translateX: '0%' },
        { ...keyframe, translateX: '100%' },
      ],
      250,
      function () {
        this.clearAnimation(selector, { translateX: true }, () => {
          if (typeof callback === 'function') {
            callback();
          }
        });
      }.bind(this.content),
    );
  },
 
  slideIn(i, callback) {
    const selector = `${this.selector}-${i}`;
    const keyframe = this._getCurrentKeyframe(i);
    this.content.animate(
      selector,
      [
        { ...keyframe, translateX: '100%' },
        { ...keyframe, translateX: '0%' },
      ],
      250,
      function () {
        this.clearAnimation(selector, { translateX: true }, () => {
          if (typeof callback === 'function') {
            callback();
          }
        });
      }.bind(this.content),
    );
  },
 
  // 获取动画结束Y轴位置
  _getEndY(index, callback) {
    wx.createSelectorQuery()
      .in(this.content)
      .select(`${this.selector}-${index}`)
      .boundingClientRect(res => {
        callback(res.height);
        this.toggleList.push(true);
      })
      .exec();
  },
 
  _getCurrentKeyframe(i) {
    let keyframe;
    if (this.expanded) {
      keyframe = this.keyframes[i][1];
    } else {
      keyframe = this.keyframes[i][0];
    }
    return keyframe;
  },
};
 
function animation1(options) {
  return new ZTranslate(options);
}
 
export { animation1 };