Skip to content

Commit

Permalink
Merge pull request #59 from LottieFiles/fix/window
Browse files Browse the repository at this point in the history
fix: changed way window is used
  • Loading branch information
samuelOsborne authored Feb 17, 2022
2 parents 9d35858 + 50c0712 commit 1712703
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@lottiefiles/lottie-interactivity",
"description": "This is a small effects and interactivity library written to be paired with the Lottie Web Player",
"version": "0.1.5",
"version": "1.3.5",
"license": "MIT",
"main": "./dist/lottie-interactivity.min.js",
"module": "./dist/lottie-interactivity.es.js",
Expand Down
17 changes: 10 additions & 7 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,18 @@ export class LottieInteractivity {
this.playCounter = 0;
this.stateHandler = new Map();
this.transitionHandler = new Map();

//globals
this.window = this.container.ownerDocument.defaultView;
}

getContainerVisibility() {
// Get the bounding box for the lottie player or container
const { top, height } = this.container.getBoundingClientRect();

// Calculate current view percentage
const current = window.innerHeight - top;
const max = window.innerHeight + height;
const current = this.window.innerHeight - top;
const max = this.window.innerHeight + height;
return current / max;
}

Expand All @@ -81,7 +84,7 @@ export class LottieInteractivity {

initScrollMode() {
this.player.stop();
window.addEventListener('scroll', this.#scrollHandler);
this.window.addEventListener('scroll', this.#scrollHandler, true);
}

initCursorMode() {
Expand Down Expand Up @@ -227,7 +230,7 @@ export class LottieInteractivity {

stop() {
if (this.mode === 'scroll') {
window.removeEventListener('scroll', this.#scrollHandler);
this.window.removeEventListener('scroll', this.#scrollHandler, true);
}

if (this.mode === 'cursor') {
Expand Down Expand Up @@ -670,22 +673,22 @@ export class LottieInteractivity {
throw new Error(`${ERROR_PREFIX} Specified player is invalid.`, this.enteredPlayer);
}
} else {
if (window.lottie) {
if (this.window.lottie) {
this.stop();
this.player.destroy();
// Removes svg animation contained inside
this.container.innerHTML = "";

if (typeof path === 'object' && path.constructor.name === 'AnimationItem') {
this.player = window.lottie.loadAnimation({
this.player = this.window.lottie.loadAnimation({
loop: false,
autoplay: false,
animationData: path.animationData,
container: this.container
});
}
else {
this.player = window.lottie.loadAnimation({
this.player = this.window.lottie.loadAnimation({
loop: false,
autoplay: false,
path,
Expand Down

0 comments on commit 1712703

Please sign in to comment.