# scroll

# progress

If the element is in the visible range, process according to the progress rate.

progress:
0
class ScrollRevealProgress {

  constructor(target, callbackIn, callbackOut, offset = 150, wrapper = window, always = true) {

    /**
     * ターゲット要素
     * @type {HTMLElement}
     */
    this.target = target;

    /**
     * ラッパー要素
     * @type {HTMLElement}
     */
    this.wrapper = wrapper;

    /**
     * 要素が見え始めてからのオフセット
     * @type {number}
     */
    this.offset = offset;

    /**
     * 見え始めたときのコールバック
     * @type {Function}
     */
    this.callbackIn = typeof callbackIn === 'function' ? callbackIn : () => {
    };

    /**
     * 見切れたときのコールバック関数
     * @type {Function}
     */
    this.callbackOut = typeof callbackOut === 'function' ? callbackOut : () => {
    };

    /**
     * 常にコールバック関数を実行するかどうか
     * false だと スクロール値が変わった時にだけ実行する
     * @type {boolean}
     */
    this.always = always;

    /**
     * 可視範囲に入っているかの審議値
     * @type {boolean}
     */
    this.show = false;

    /**
     * ラッパーの高さ
     * @type {number}
     */
    this.wrapHeight = 0;

    /**
     * ラッパーからターゲットまでの距離
     * @type {number}
     */
    this.offsetTop = 0;

    /**
     * ターゲットの高さ
     * @type {number}
     */
    this.targetHeight = 0;

    /**
     * コールバック関数に渡す進捗率 0 ~ 1
     * @type {number}
     */
    this.progress = 0;

    /**
     * requestAnimationFrame の id
     * @type {number}
     */
    this.animationId = 0;

    this.init();

    return this;

  }

  get scrollY() {
    return this._scrollY;
  }

  set scrollY(value) {

    if (this._scrollY === value && !this.always) {
      return;
    }

    // スクロール値が変わった時にだけ実行
    this.check();

    this._scrollY = value;

  }

  /**
   * initialize
   */
  init() {

    const resizeHandle = () => this.resize();
    window.addEventListener('resize', resizeHandle);

    this.getItemInfo();
    this.play();

  }

  /**
   * ターゲットとラッパーの 高さと位置を取得する
   */
  getItemInfo() {

//    console.log('getItemInfo');

    // ラッパーの高さをセット
    this.wrapHeight = (this.wrapper === window) ? window.innerHeight : this.wrapper.offsetHeight;

    const rect = this.target.getBoundingClientRect();

    // ラッパーが window 以外の時はラッパーのオフセットを引く
    if (this.wrapper === window) {

      this.offsetTop = rect.top;

    } else {

      const _rect = this.wrapper.getBoundingClientRect();
      this.offsetTop = rect.top - _rect.top;

    }

    //ターゲットの高さをセット
    this.targetHeight = this.target.offsetHeight;

  }

  play() {

    // スクロール値
    this.scrollY = this.wrapper === window ? this.wrapper.pageYOffset : this.wrapper.scrollTop;

    this.animationId = requestAnimationFrame(() => this.play());

  }

  check() {

    this.show = (this.scrollY + this.wrapHeight > this.offsetTop + this.offset) && (this.scrollY < this.offsetTop + this.targetHeight);
    const value = this.scrollY + this.wrapHeight - this.offsetTop - this.offset;

    this.progress = value / (this.wrapHeight - this.offset + this.targetHeight);

    if (this.show) {

      this.callbackIn.call(this, this.progress);

    } else {

      this.callbackOut.call(this, this.progress);

    }

  }

  resize() {
    this.getItemInfo();
  }

  destroy() {

    cancelAnimationFrame(this.animationId);

    const resizeHandle = () => this.resize();
    window.removeEventListener('resize', resizeHandle);

    this.target = null;
    this.wrapper = null;
    this.offset = 0;
    this.callbackIn = null;
    this.callbackOut = null;
    this.show = false;
    this.wrapHeight = 0;
    this.offsetTop = 0;
    this.targetHeight = 0;
    this.progress = 0;
    this.animationId = 0;

  }

}

export default ScrollRevealProgress;


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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

# color

background color
import ScrollRevealScript from './ScrollRevealScript';
import math from './math';
import chroma from 'chroma-js';
import {TweenMax} from 'gsap';

new ScrollRevealScript(this.$refs.target, progress => {

  TweenMax.set(this.$refs.target, {
    backgroundColor: chroma.mix( chroma.hsl(110, .75, math.map(progress, 0, 1, .45, .7)), chroma.hsl(240, .75, math.map(progress, 0, 1, .45, .7)), progress, 'hsl').css()
  })

}, null, 100, this.$refs.wrap);

1
2
3
4
5
6
7
8
9
10
11
12
13

# transform

Transform elements according to scroll.
Apply easing to transforming changes

transform
const easing = .1;
const updateValue = {
  scaleY: 0,
  rotationX: 0,
  y: 0,
};

new ScrollRevealScript(this.$refs.target, progress => {

  updateValue.scaleY += (math.map(Math.abs(progress - .5), 0, .5, 1, 1.1) - updateValue.scaleY) * easing;
  updateValue.rotationX += (math.map(progress, 0, 1, 60, -60) - updateValue.rotationX) * easing;
  updateValue.y += (math.map(progress, 0, 1, 100, -100) - updateValue.y) * easing;

  TweenMax.set(this.$refs.target, {
    scaleY: updateValue.scaleY,
    rotationX: updateValue.rotationX,
    y: updateValue.y,
  });

}, null, -150, this.$refs.wrap);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Last Updated: 11/18/2019, 12:17:28 PM