@@ -4,6 +4,9 @@ import { Scroll } from './Scroll';
4
4
import { Element } from './Element' ;
5
5
import { VERTICAL } from '../constants' ;
6
6
import { testForPassiveScroll } from '../utils/testForPassiveScroll' ;
7
+ import { ParallaxControllerType } from '../types' ;
8
+
9
+ type ViewElement = HTMLElement | Window ;
7
10
8
11
/**
9
12
* -------------------------------------------------------
@@ -17,15 +20,26 @@ import { testForPassiveScroll } from '../utils/testForPassiveScroll';
17
20
* based on x/y offsets and current scroll position.
18
21
*
19
22
*/
20
- export function ParallaxController ( { scrollAxis = VERTICAL , scrollContainer } ) {
23
+
24
+ export type ParallaxControllerOptions = {
25
+ scrollAxis ?: 'vertical' | 'horizontal' ;
26
+ scrollContainer ?: HTMLElement ;
27
+ } ;
28
+
29
+ export function ParallaxController ( {
30
+ scrollAxis = VERTICAL ,
31
+ scrollContainer,
32
+ } : ParallaxControllerOptions ) {
21
33
// All parallax elements to be updated
22
34
let elements = [ ] ;
23
35
24
36
let hasScrollContainer = ! ! scrollContainer ;
25
- let viewEl = scrollContainer || window ;
37
+ let viewEl : ViewElement = scrollContainer || window ;
26
38
27
39
// Scroll and View
40
+ // @ts -expect-error
28
41
const x = hasScrollContainer ? viewEl . scrollLeft : window . pageXOffset ;
42
+ // @ts -expect-error
29
43
const y = hasScrollContainer ? viewEl . scrollTop : window . pageYOffset ;
30
44
const scroll = new Scroll ( x , y ) ;
31
45
let view = new View ( { width : 0 , height : 0 , scrollContainer } ) ;
@@ -36,7 +50,7 @@ export function ParallaxController({ scrollAxis = VERTICAL, scrollContainer }) {
36
50
// Passive support
37
51
const supportsPassive = testForPassiveScroll ( ) ;
38
52
39
- function _addListeners ( el ) {
53
+ function _addListeners ( el : ViewElement ) {
40
54
el . addEventListener (
41
55
'scroll' ,
42
56
_handleScroll ,
@@ -45,12 +59,8 @@ export function ParallaxController({ scrollAxis = VERTICAL, scrollContainer }) {
45
59
window . addEventListener ( 'resize' , _handleResize , false ) ;
46
60
}
47
61
48
- function _removeListeners ( el ) {
49
- el . removeEventListener (
50
- 'scroll' ,
51
- _handleScroll ,
52
- supportsPassive ? { passive : true } : false
53
- ) ;
62
+ function _removeListeners ( el : ViewElement ) {
63
+ el . removeEventListener ( 'scroll' , _handleScroll , false ) ;
54
64
window . removeEventListener ( 'resize' , _handleResize , false ) ;
55
65
}
56
66
@@ -64,7 +74,9 @@ export function ParallaxController({ scrollAxis = VERTICAL, scrollContainer }) {
64
74
function _handleScroll ( ) {
65
75
// Save current scroll
66
76
// Supports IE 9 and up.
77
+ // @ts -expect-error
67
78
const nx = hasScrollContainer ? viewEl . scrollLeft : window . pageXOffset ;
79
+ // @ts -expect-error
68
80
const ny = hasScrollContainer ? viewEl . scrollTop : window . pageYOffset ;
69
81
scroll . setScroll ( nx , ny ) ;
70
82
@@ -92,7 +104,7 @@ export function ParallaxController({ scrollAxis = VERTICAL, scrollContainer }) {
92
104
* attributes, if so set the elements parallax styles.
93
105
*/
94
106
// @ts -ignore
95
- function _updateAllElements ( { updateCache } = { } ) {
107
+ function _updateAllElements ( { updateCache } : { updateCache : boolean } = { } ) {
96
108
if ( elements ) {
97
109
elements . forEach ( ( element ) => {
98
110
_updateElementPosition ( element ) ;
@@ -120,7 +132,9 @@ export function ParallaxController({ scrollAxis = VERTICAL, scrollContainer }) {
120
132
*/
121
133
function _setViewSize ( ) {
122
134
if ( hasScrollContainer ) {
135
+ // @ts -ignore
123
136
const width = viewEl . offsetWidth ;
137
+ // @ts -ignore
124
138
const height = viewEl . offsetHeight ;
125
139
return view . setSize ( width , height ) ;
126
140
}
@@ -142,7 +156,7 @@ export function ParallaxController({ scrollAxis = VERTICAL, scrollContainer }) {
142
156
* Gets the parallax elements in the controller
143
157
* @return {array } parallax elements
144
158
*/
145
- this . getElements = function ( ) {
159
+ this . getElements = function ( ) : Element [ ] {
146
160
return elements ;
147
161
} ;
148
162
@@ -231,7 +245,9 @@ export function ParallaxController({ scrollAxis = VERTICAL, scrollContainer }) {
231
245
* Static method to instantiate the ParallaxController.
232
246
* @returns {Object } ParallaxController
233
247
*/
234
- ParallaxController . init = function ( options ) {
248
+ ParallaxController . init = function (
249
+ options : ParallaxControllerOptions
250
+ ) : ParallaxControllerType {
235
251
const hasWindow = typeof window !== 'undefined' ;
236
252
237
253
if ( ! hasWindow ) {
0 commit comments