@@ -9,7 +9,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
9
9
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
10
*/
11
11
12
- import { entrySatisfiesRatio } from './utils' ;
12
+ import { calculateIsIntersecting } from './utils' ;
13
13
14
14
import { Frame , QueueDOMElementInterface , DOMQueue , ElementScheduler , Engine , generateToken } from './metal/index' ;
15
15
@@ -65,7 +65,7 @@ export class SpanielIntersectionObserver implements IntersectionObserver {
65
65
public thresholds : number [ ] ;
66
66
private records : { [ index : string ] : EntryEvent } ;
67
67
68
- observe ( target : Element ) {
68
+ observe ( target : HTMLElement ) {
69
69
let trackedTarget = target as SpanielTrackedElement ;
70
70
71
71
let id = ( trackedTarget . __spanielId = trackedTarget . __spanielId || generateToken ( ) ) ;
@@ -79,7 +79,7 @@ export class SpanielIntersectionObserver implements IntersectionObserver {
79
79
) ;
80
80
return id ;
81
81
}
82
- private onTick ( frame : Frame , id : string , clientRect : DOMRectReadOnly , el : Element ) {
82
+ private onTick ( frame : Frame , id : string , clientRect : DOMRectReadOnly , el : SpanielTrackedElement ) {
83
83
let { numSatisfiedThresholds, entry } = this . generateEntryEvent ( frame , clientRect , el ) ;
84
84
let record : EntryEvent =
85
85
this . records [ id ] ||
@@ -88,8 +88,12 @@ export class SpanielIntersectionObserver implements IntersectionObserver {
88
88
numSatisfiedThresholds : 0
89
89
} ) ;
90
90
91
- if ( numSatisfiedThresholds !== record . numSatisfiedThresholds ) {
91
+ if (
92
+ numSatisfiedThresholds !== record . numSatisfiedThresholds ||
93
+ entry . isIntersecting !== record . entry . isIntersecting
94
+ ) {
92
95
record . numSatisfiedThresholds = numSatisfiedThresholds ;
96
+ record . entry = entry ;
93
97
this . scheduler . scheduleWork ( ( ) => {
94
98
this . callback ( [ entry ] ) ;
95
99
} ) ;
@@ -106,14 +110,14 @@ export class SpanielIntersectionObserver implements IntersectionObserver {
106
110
takeRecords ( ) : IntersectionObserverEntry [ ] {
107
111
return [ ] ;
108
112
}
109
- private generateEntryEvent ( frame : Frame , clientRect : DOMRectReadOnly , el : Element ) : EntryEvent {
113
+ private generateEntryEvent ( frame : Frame , clientRect : DOMRectReadOnly , el : HTMLElement ) : EntryEvent {
110
114
let count : number = 0 ;
111
115
let entry = generateEntry ( frame , clientRect , el , this . rootMarginObj ) ;
112
116
let ratio = entry . intersectionRatio ;
113
117
114
118
for ( let i = 0 ; i < this . thresholds . length ; i ++ ) {
115
119
let threshold = this . thresholds [ i ] ;
116
- if ( entrySatisfiesRatio ( entry , threshold ) ) {
120
+ if ( entry . intersectionRatio >= threshold ) {
117
121
count ++ ;
118
122
}
119
123
}
@@ -152,7 +156,7 @@ function addRatio(entryInit: SpanielIntersectionObserverEntryInit): Intersection
152
156
intersectionRect,
153
157
target,
154
158
intersectionRatio,
155
- isIntersecting : intersectionRatio > 0
159
+ isIntersecting : calculateIsIntersecting ( { intersectionRect } )
156
160
} ;
157
161
}
158
162
@@ -182,13 +186,37 @@ export class IntersectionObserverEntry implements IntersectionObserverEntryInit
182
186
};
183
187
*/
184
188
189
+ function emptyRect ( ) : ClientRect | DOMRect {
190
+ return {
191
+ bottom : 0 ,
192
+ height : 0 ,
193
+ left : 0 ,
194
+ right : 0 ,
195
+ top : 0 ,
196
+ width : 0 ,
197
+ x : 0 ,
198
+ y : 0
199
+ } ;
200
+ }
201
+
185
202
export function generateEntry (
186
203
frame : Frame ,
187
204
clientRect : DOMRectReadOnly ,
188
- el : Element ,
205
+ el : HTMLElement ,
189
206
rootMargin : DOMMargin
190
207
) : IntersectionObserverEntry {
191
- let { top, bottom, left, right } = clientRect ;
208
+ if ( el . style . display === 'none' ) {
209
+ return {
210
+ boundingClientRect : emptyRect ( ) ,
211
+ intersectionRatio : 0 ,
212
+ intersectionRect : emptyRect ( ) ,
213
+ isIntersecting : false ,
214
+ rootBounds : emptyRect ( ) ,
215
+ target : el ,
216
+ time : frame . timestamp
217
+ } ;
218
+ }
219
+ let { bottom, right } = clientRect ;
192
220
let rootBounds : ClientRect = {
193
221
left : frame . left + rootMargin . left ,
194
222
top : frame . top + rootMargin . top ,
0 commit comments