Skip to content

Commit b0bbb18

Browse files
author
Marcelo Tosco @capynet
committed
Bugfix: Added an initial check when the plugin gets initialized.
1 parent 4b2b920 commit b0bbb18

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

dist/isInViewport.jquery.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
this.$el = $(el);
1313
this.cb = cb;
1414
this.offset = offset;
15+
this.previousIsInState = false;
16+
17+
// Make the first check
18+
this.check();
19+
20+
// Start watching.
1521
this.watch();
22+
1623
return this;
1724
};
1825

@@ -23,7 +30,7 @@
2330
*
2431
* @returns {boolean}
2532
*/
26-
isIn: function isIn() {
33+
isIn: function () {
2734
const _self = this;
2835
const $win = $(window);
2936
const elementTop = _self.$el.offset().top - _self.offset;
@@ -38,20 +45,24 @@
3845
*/
3946
watch: function () {
4047
const self = this;
41-
let isIn = false;
48+
$(window).on('resize scroll', self.check.bind(self));
49+
},
4250

43-
$(window).on('resize scroll', function () {
51+
/**
52+
* Checks if the element is on in the viewport.
53+
*/
54+
check: function () {
55+
const self = this;
4456

45-
if (self.isIn() && isIn === false) {
46-
self.cb.call(self.$el, 'entered');
47-
isIn = true;
48-
}
57+
if (self.isIn() && self.previousIsInState === false) {
58+
self.cb.call(self.$el, 'entered');
59+
self.previousIsInState = true;
60+
}
4961

50-
if (isIn === true && !self.isIn()) {
51-
self.cb.call(self.$el, 'leaved');
52-
isIn = false;
53-
}
54-
})
62+
if (self.previousIsInState === true && !self.isIn()) {
63+
self.cb.call(self.$el, 'leaved');
64+
self.previousIsInState = false;
65+
}
5566
}
5667
};
5768

@@ -69,4 +80,4 @@
6980
})
7081
}
7182

72-
}(window.jQuery);
83+
}(window.jQuery);

dist/isInViewport.jquery.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

isInViewport.jquery.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
this.$el = $(el);
1313
this.cb = cb;
1414
this.offset = offset;
15+
this.previousIsInState = false;
16+
17+
// Make the first check
18+
this.check();
19+
20+
// Start watching.
1521
this.watch();
22+
1623
return this;
1724
};
1825

@@ -23,7 +30,7 @@
2330
*
2431
* @returns {boolean}
2532
*/
26-
isIn: function isIn() {
33+
isIn: function () {
2734
const _self = this;
2835
const $win = $(window);
2936
const elementTop = _self.$el.offset().top - _self.offset;
@@ -38,20 +45,24 @@
3845
*/
3946
watch: function () {
4047
const self = this;
41-
let isIn = false;
48+
$(window).on('resize scroll', self.check.bind(self));
49+
},
4250

43-
$(window).on('resize scroll', function () {
51+
/**
52+
* Checks if the element is on in the viewport.
53+
*/
54+
check: function () {
55+
const self = this;
4456

45-
if (self.isIn() && isIn === false) {
46-
self.cb.call(self.$el, 'entered');
47-
isIn = true;
48-
}
57+
if (self.isIn() && self.previousIsInState === false) {
58+
self.cb.call(self.$el, 'entered');
59+
self.previousIsInState = true;
60+
}
4961

50-
if (isIn === true && !self.isIn()) {
51-
self.cb.call(self.$el, 'leaved');
52-
isIn = false;
53-
}
54-
})
62+
if (self.previousIsInState === true && !self.isIn()) {
63+
self.cb.call(self.$el, 'leaved');
64+
self.previousIsInState = false;
65+
}
5566
}
5667
};
5768

@@ -69,4 +80,4 @@
6980
})
7081
}
7182

72-
}(window.jQuery);
83+
}(window.jQuery);

0 commit comments

Comments
 (0)