Skip to content

Commit

Permalink
Bugfix: Added an initial check when the plugin gets initialized.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcelo Tosco @capynet committed Apr 7, 2020
1 parent 4b2b920 commit b0bbb18
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
37 changes: 24 additions & 13 deletions dist/isInViewport.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
this.$el = $(el);
this.cb = cb;
this.offset = offset;
this.previousIsInState = false;

// Make the first check
this.check();

// Start watching.
this.watch();

return this;
};

Expand All @@ -23,7 +30,7 @@
*
* @returns {boolean}
*/
isIn: function isIn() {
isIn: function () {
const _self = this;
const $win = $(window);
const elementTop = _self.$el.offset().top - _self.offset;
Expand All @@ -38,20 +45,24 @@
*/
watch: function () {
const self = this;
let isIn = false;
$(window).on('resize scroll', self.check.bind(self));
},

$(window).on('resize scroll', function () {
/**
* Checks if the element is on in the viewport.
*/
check: function () {
const self = this;

if (self.isIn() && isIn === false) {
self.cb.call(self.$el, 'entered');
isIn = true;
}
if (self.isIn() && self.previousIsInState === false) {
self.cb.call(self.$el, 'entered');
self.previousIsInState = true;
}

if (isIn === true && !self.isIn()) {
self.cb.call(self.$el, 'leaved');
isIn = false;
}
})
if (self.previousIsInState === true && !self.isIn()) {
self.cb.call(self.$el, 'leaved');
self.previousIsInState = false;
}
}
};

Expand All @@ -69,4 +80,4 @@
})
}

}(window.jQuery);
}(window.jQuery);
2 changes: 1 addition & 1 deletion dist/isInViewport.jquery.min.js

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

37 changes: 24 additions & 13 deletions isInViewport.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
this.$el = $(el);
this.cb = cb;
this.offset = offset;
this.previousIsInState = false;

// Make the first check
this.check();

// Start watching.
this.watch();

return this;
};

Expand All @@ -23,7 +30,7 @@
*
* @returns {boolean}
*/
isIn: function isIn() {
isIn: function () {
const _self = this;
const $win = $(window);
const elementTop = _self.$el.offset().top - _self.offset;
Expand All @@ -38,20 +45,24 @@
*/
watch: function () {
const self = this;
let isIn = false;
$(window).on('resize scroll', self.check.bind(self));
},

$(window).on('resize scroll', function () {
/**
* Checks if the element is on in the viewport.
*/
check: function () {
const self = this;

if (self.isIn() && isIn === false) {
self.cb.call(self.$el, 'entered');
isIn = true;
}
if (self.isIn() && self.previousIsInState === false) {
self.cb.call(self.$el, 'entered');
self.previousIsInState = true;
}

if (isIn === true && !self.isIn()) {
self.cb.call(self.$el, 'leaved');
isIn = false;
}
})
if (self.previousIsInState === true && !self.isIn()) {
self.cb.call(self.$el, 'leaved');
self.previousIsInState = false;
}
}
};

Expand All @@ -69,4 +80,4 @@
})
}

}(window.jQuery);
}(window.jQuery);

0 comments on commit b0bbb18

Please sign in to comment.