|
1 |
| -import { createId } from '../utils/index'; |
2 |
| -import { |
3 |
| - getOffsets, |
4 |
| - isElementInView, |
5 |
| - percentMoved, |
6 |
| - setParallaxStyles, |
7 |
| -} from '../helpers/index'; |
8 | 1 | import { VERTICAL } from '../constants';
|
9 | 2 | import Bounds from './Bounds';
|
10 | 3 | import Rect from './Rect';
|
11 | 4 | import { ParallaxStartEndOffsets } from '../types';
|
| 5 | +import getOffsets from '../helpers/getOffsets'; |
| 6 | +import { isElementInView } from '../helpers/isElementInView'; |
| 7 | +import { percentMoved } from '../helpers/percentMoved'; |
| 8 | +import { setParallaxStyles } from '../helpers/elementStyles'; |
| 9 | +import { createId } from '../utils/createId'; |
12 | 10 |
|
13 | 11 | export class Element {
|
14 |
| - elInner: HTMLElement; |
15 |
| - elOuter: HTMLElement; |
16 |
| - // TODO |
17 |
| - props: any; |
18 |
| - scrollAxis: 'vertical' | 'horizontal'; |
19 |
| - id: number; |
20 |
| - offsets: ParallaxStartEndOffsets; |
21 |
| - isInView: boolean; |
22 |
| - percent: number; |
23 |
| - |
24 |
| - updatePosition: (view: any, scroll: any) => void; |
25 |
| - |
26 |
| - rect: Rect; |
27 |
| - bounds: Bounds; |
28 |
| - |
29 |
| - constructor(options) { |
30 |
| - this.elInner = options.elInner; |
31 |
| - this.elOuter = options.elOuter; |
32 |
| - this.props = options.props; |
33 |
| - this.scrollAxis = options.scrollAxis; |
34 |
| - this.id = createId(); |
35 |
| - this.offsets = getOffsets(this.props); |
36 |
| - this.isInView = null; |
37 |
| - this.percent = 0; |
38 |
| - |
39 |
| - this.updatePosition = |
40 |
| - options.scrollAxis === VERTICAL |
41 |
| - ? this._updatePositionVertical |
42 |
| - : this._updatePositionHorizontal; |
43 |
| - } |
44 |
| - |
45 |
| - updateProps(nextProps) { |
46 |
| - this.props = { ...this.props, ...nextProps }; |
47 |
| - this.offsets = getOffsets(nextProps); |
48 |
| - return this; |
49 |
| - } |
50 |
| - |
51 |
| - setCachedAttributes(view, scroll) { |
52 |
| - this.rect = new Rect(this.elOuter, view, scroll); |
53 |
| - this.bounds = new Bounds(this.rect, this.offsets, view); |
54 |
| - return this; |
55 |
| - } |
56 |
| - |
57 |
| - _updatePositionHorizontal(view, scroll) { |
58 |
| - this.isInView = isElementInView( |
59 |
| - this.bounds.left, |
60 |
| - this.bounds.right, |
61 |
| - view.width, |
62 |
| - scroll.x |
63 |
| - ); |
64 |
| - |
65 |
| - if (!this.isInView) return this; |
66 |
| - |
67 |
| - this.percent = percentMoved( |
68 |
| - this.rect.left, |
69 |
| - this.rect.originTotalDistX, |
70 |
| - view.width, |
71 |
| - scroll.x |
72 |
| - ); |
73 |
| - |
74 |
| - setParallaxStyles(this.elInner, this.offsets, this.percent); |
75 |
| - |
76 |
| - return this; |
77 |
| - } |
78 |
| - |
79 |
| - _updatePositionVertical(view, scroll) { |
80 |
| - this.isInView = isElementInView( |
81 |
| - this.bounds.top, |
82 |
| - this.bounds.bottom, |
83 |
| - view.height, |
84 |
| - scroll.y |
85 |
| - ); |
86 |
| - |
87 |
| - if (!this.isInView) return this; |
88 |
| - |
89 |
| - this.percent = percentMoved( |
90 |
| - this.rect.top, |
91 |
| - this.rect.originTotalDistY, |
92 |
| - view.height, |
93 |
| - scroll.y |
94 |
| - ); |
95 |
| - |
96 |
| - setParallaxStyles(this.elInner, this.offsets, this.percent); |
97 |
| - |
98 |
| - return this; |
99 |
| - } |
| 12 | + elInner: HTMLElement; |
| 13 | + elOuter: HTMLElement; |
| 14 | + // TODO |
| 15 | + props: any; |
| 16 | + scrollAxis: 'vertical' | 'horizontal'; |
| 17 | + id: number; |
| 18 | + offsets: ParallaxStartEndOffsets; |
| 19 | + isInView: boolean; |
| 20 | + percent: number; |
| 21 | + |
| 22 | + updatePosition: (view: any, scroll: any) => void; |
| 23 | + |
| 24 | + rect: Rect; |
| 25 | + bounds: Bounds; |
| 26 | + |
| 27 | + constructor(options) { |
| 28 | + this.elInner = options.elInner; |
| 29 | + this.elOuter = options.elOuter; |
| 30 | + this.props = options.props; |
| 31 | + this.scrollAxis = options.scrollAxis; |
| 32 | + this.id = createId(); |
| 33 | + this.offsets = getOffsets(this.props); |
| 34 | + this.isInView = null; |
| 35 | + this.percent = 0; |
| 36 | + |
| 37 | + this.updatePosition = |
| 38 | + options.scrollAxis === VERTICAL |
| 39 | + ? this._updatePositionVertical |
| 40 | + : this._updatePositionHorizontal; |
| 41 | + } |
| 42 | + |
| 43 | + updateProps(nextProps) { |
| 44 | + this.props = { ...this.props, ...nextProps }; |
| 45 | + this.offsets = getOffsets(nextProps); |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + setCachedAttributes(view, scroll) { |
| 50 | + this.rect = new Rect(this.elOuter, view, scroll); |
| 51 | + this.bounds = new Bounds(this.rect, this.offsets, view); |
| 52 | + return this; |
| 53 | + } |
| 54 | + |
| 55 | + _updatePositionHorizontal(view, scroll) { |
| 56 | + this.isInView = isElementInView( |
| 57 | + this.bounds.left, |
| 58 | + this.bounds.right, |
| 59 | + view.width, |
| 60 | + scroll.x |
| 61 | + ); |
| 62 | + |
| 63 | + if (!this.isInView) return this; |
| 64 | + |
| 65 | + this.percent = percentMoved( |
| 66 | + this.rect.left, |
| 67 | + this.rect.originTotalDistX, |
| 68 | + view.width, |
| 69 | + scroll.x |
| 70 | + ); |
| 71 | + |
| 72 | + setParallaxStyles(this.elInner, this.offsets, this.percent); |
| 73 | + |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + _updatePositionVertical(view, scroll) { |
| 78 | + this.isInView = isElementInView( |
| 79 | + this.bounds.top, |
| 80 | + this.bounds.bottom, |
| 81 | + view.height, |
| 82 | + scroll.y |
| 83 | + ); |
| 84 | + |
| 85 | + if (!this.isInView) return this; |
| 86 | + |
| 87 | + this.percent = percentMoved( |
| 88 | + this.rect.top, |
| 89 | + this.rect.originTotalDistY, |
| 90 | + view.height, |
| 91 | + scroll.y |
| 92 | + ); |
| 93 | + |
| 94 | + setParallaxStyles(this.elInner, this.offsets, this.percent); |
| 95 | + |
| 96 | + return this; |
| 97 | + } |
100 | 98 | }
|
0 commit comments