Skip to content

Commit

Permalink
scroll spy run once option (#282)
Browse files Browse the repository at this point in the history
* Run only once

* Update options in readme file

Co-authored-by: Adi Cacan <[email protected]>
  • Loading branch information
adicacan and Adi Cacan authored May 18, 2022
1 parent 0e4a9ae commit d5721ed
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ interface CountUpOptions {
numerals?: string[]; // numeral glyph substitution
enableScrollSpy?: boolean; // start animation when target is in view
scrollSpyDelay?: number; // delay (ms) after target comes into view
scrollSpyOnce?: boolean; // run only once
}
```

Expand Down
2 changes: 2 additions & 0 deletions dist/countUp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface CountUpOptions {
numerals?: string[];
enableScrollSpy?: boolean;
scrollSpyDelay?: number;
scrollSpyOnce?: boolean;
}
export declare class CountUp {
private endVal;
Expand All @@ -36,6 +37,7 @@ export declare class CountUp {
duration: number;
paused: boolean;
frameVal: number;
once: boolean;
constructor(target: string | HTMLElement | HTMLInputElement, endVal: number, options?: CountUpOptions);
handleScroll(self: CountUp): void;
private determineDirectionAndSmartEasing;
Expand Down
6 changes: 5 additions & 1 deletion dist/countUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ var CountUp = /** @class */ (function () {
suffix: '',
enableScrollSpy: false,
scrollSpyDelay: 200,
scrollSpyOnce: false,
};
this.finalEndVal = null; // for smart easing
this.useEasing = true;
this.countDown = false;
this.error = '';
this.startVal = 0;
this.paused = true;
this.once = false;
this.count = function (timestamp) {
if (!_this.startTime) {
_this.startTime = timestamp;
Expand Down Expand Up @@ -153,14 +155,16 @@ var CountUp = /** @class */ (function () {
}
}
CountUp.prototype.handleScroll = function (self) {
if (!self || !window)
if (!self || !window || self.once)
return;
var bottomOfScroll = window.innerHeight + window.scrollY;
var bottomOfEl = self.el.offsetTop + self.el.offsetHeight;
if (bottomOfEl < bottomOfScroll && bottomOfEl > window.scrollY && self.paused) {
// in view
self.paused = false;
setTimeout(function () { return self.start(); }, self.options.scrollSpyDelay);
if (self.options.scrollSpyOnce)
self.once = true;
}
else if (window.scrollY > bottomOfEl && !self.paused) {
// scrolled past
Expand Down
2 changes: 1 addition & 1 deletion dist/countUp.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion dist/countUp.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@
suffix: '',
enableScrollSpy: false,
scrollSpyDelay: 200,
scrollSpyOnce: false,
};
this.finalEndVal = null; // for smart easing
this.useEasing = true;
this.countDown = false;
this.error = '';
this.startVal = 0;
this.paused = true;
this.once = false;
this.count = function (timestamp) {
if (!_this.startTime) {
_this.startTime = timestamp;
Expand Down Expand Up @@ -159,14 +161,16 @@
}
}
CountUp.prototype.handleScroll = function (self) {
if (!self || !window)
if (!self || !window || self.once)
return;
var bottomOfScroll = window.innerHeight + window.scrollY;
var bottomOfEl = self.el.offsetTop + self.el.offsetHeight;
if (bottomOfEl < bottomOfScroll && bottomOfEl > window.scrollY && self.paused) {
// in view
self.paused = false;
setTimeout(function () { return self.start(); }, self.options.scrollSpyDelay);
if (self.options.scrollSpyOnce)
self.once = true;
}
else if (window.scrollY > bottomOfEl && !self.paused) {
// scrolled past
Expand Down
Loading

0 comments on commit d5721ed

Please sign in to comment.