Skip to content

Commit b28be7c

Browse files
committed
Merge branch 'requestAnimationFrame' of https://github.com/NormanV41/ngx-page-scroll into NormanV41-requestAnimationFrame
2 parents 13eec46 + 6754529 commit b28be7c

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

projects/ngx-page-scroll-core/src/lib/page-scroll-instance.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ export class PageScrollInstance {
8080
/* Whether an interrupt listener is attached to the body or not */
8181
public interruptListenersAttached = false;
8282

83-
/* References to the timer instance that is used to perform the scroll animation to be
83+
/* Value of the requestAnimationFrameId that is used to perform the scroll animation to be
8484
able to clear it on animation end*/
85-
public timer: any = null;
85+
86+
public requestFrameId: number | null = null;
8687

8788
/**
8889
* Private constructor, requires the properties assumed to be the bare minimum.

projects/ngx-page-scroll-core/src/lib/providers/ngx-page-scroll.service.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ export class PageScrollService {
5454
pageScrollInstance.detachInterruptListeners();
5555
}
5656

57-
if (pageScrollInstance.timer) {
57+
if (pageScrollInstance.requestFrameId) {
5858
// Clear/Stop the timer
59-
clearInterval(pageScrollInstance.timer);
59+
window.cancelAnimationFrame(pageScrollInstance.requestFrameId)
6060
// Clear the reference to this timer
61-
pageScrollInstance.timer = undefined;
61+
pageScrollInstance.requestFrameId = null;
6262
pageScrollInstance.fireEvent(!interrupted);
6363

6464
return true;
@@ -198,7 +198,14 @@ export class PageScrollService {
198198
// .. and calculate the end time (when we need to finish at last)
199199
pageScrollInstance.endTime = pageScrollInstance.startTime + pageScrollInstance.executionDuration;
200200

201-
pageScrollInstance.timer = setInterval((instance: PageScrollInstance) => {
201+
pageScrollInstance.requestFrameId = window.requestAnimationFrame(this.updateScrollPostion(pageScrollInstance))
202+
203+
// Register the instance as running one
204+
this.runningInstances.push(pageScrollInstance);
205+
}
206+
207+
public updateScrollPostion(instance: PageScrollInstance){
208+
return ()=>{
202209
// Take the current time
203210
const currentTime: number = new Date().getTime();
204211

@@ -231,12 +238,10 @@ export class PageScrollService {
231238
// (otherwise the event might arrive at "too early")
232239
if (stopNow) {
233240
this.stopInternal(false, instance);
241+
} else{
242+
window.requestAnimationFrame(this.updateScrollPostion(instance))
234243
}
235-
236-
}, this.config._interval, pageScrollInstance);
237-
238-
// Register the instance as running one
239-
this.runningInstances.push(pageScrollInstance);
244+
}
240245
}
241246

242247
public scroll(options: PageScrollOptions): void {

0 commit comments

Comments
 (0)