Skip to content

Commit 4b2b920

Browse files
author
Marcelo Tosco @capynet
committed
- Added offset param.
- Updated gulpfile.js and packeage.json. - Review the original code.
1 parent 81779db commit 4b2b920

File tree

7 files changed

+2611
-72
lines changed

7 files changed

+2611
-72
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ You need throw confetti when the user scroll and see an element?, so this plugin
88
npm install jquery-is-in-viewport --save
99
// or
1010
yarn add jquery-is-in-viewport
11-
// or
12-
bower install jquery-is-in-viewport
1311
```
1412

1513
## Usage
1614
```javascript
15+
const offset = 100;
16+
1717
$('.block').isInViewport(function (status) {
1818
if (status === 'entered') {
1919
$(this).addClass('throw-confetti')
@@ -22,8 +22,9 @@ $('.block').isInViewport(function (status) {
2222
if (status === 'leaved') {
2323
$(this).removeClass('throw-confetti')
2424
}
25-
})
25+
}, offset)
2626
```
2727

2828
## Options
2929
Status can be '**entered**' or '**leaved**'.
30+

dist/isInViewport.jquery.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,72 @@
11
/* ====================================================
2-
* jQuery Is In Viewport.
2+
* jQuery is in viewport.
3+
*
34
* https://github.com/frontid/jQueryIsInViewport
45
* Marcelo Iván Tosco (capynet)
56
* Inspired on https://stackoverflow.com/a/40658647/1413049
67
* ==================================================== */
78
!function ($) {
8-
'use strict'
9+
'use strict';
910

10-
var Class = function (el, cb) {
11+
const IsInViewport = function (el, cb, offset) {
1112
this.$el = $(el);
1213
this.cb = cb;
14+
this.offset = offset;
1315
this.watch();
1416
return this;
1517
};
1618

17-
Class.prototype = {
19+
IsInViewport.prototype = {
1820

1921
/**
2022
* Checks if the element is in.
2123
*
2224
* @returns {boolean}
2325
*/
2426
isIn: function isIn() {
25-
var _self = this;
26-
var $win = $(window);
27-
var elementTop = _self.$el.offset().top;
28-
var elementBottom = elementTop + _self.$el.outerHeight();
29-
var viewportTop = $win.scrollTop();
30-
var viewportBottom = viewportTop + $win.height();
27+
const _self = this;
28+
const $win = $(window);
29+
const elementTop = _self.$el.offset().top - _self.offset;
30+
const elementBottom = elementTop + _self.$el.outerHeight();
31+
const viewportTop = $win.scrollTop();
32+
const viewportBottom = viewportTop + $win.height();
3133
return elementBottom > viewportTop && elementTop < viewportBottom;
3234
},
3335

3436
/**
3537
* Launch a callback indicating when the element is in and when is out.
3638
*/
3739
watch: function () {
38-
var _self = this;
39-
var _isIn = false;
40+
const self = this;
41+
let isIn = false;
4042

4143
$(window).on('resize scroll', function () {
4244

43-
if (_self.isIn() && _isIn === false) {
44-
_self.cb.call(_self.$el, 'entered');
45-
_isIn = true;
45+
if (self.isIn() && isIn === false) {
46+
self.cb.call(self.$el, 'entered');
47+
isIn = true;
4648
}
4749

48-
if (_isIn === true && !_self.isIn()) {
49-
_self.cb.call(_self.$el, 'leaved');
50-
_isIn = false;
50+
if (isIn === true && !self.isIn()) {
51+
self.cb.call(self.$el, 'leaved');
52+
isIn = false;
5153
}
52-
5354
})
5455
}
55-
56-
5756
};
5857

5958
// jQuery plugin.
6059
//-----------------------------------------------------------
61-
$.fn.isInViewport = function (cb) {
60+
$.fn.isInViewport = function (cb, offset) {
61+
offset || (offset = 0);
6262
return this.each(function () {
63-
var $element = $(this);
64-
var data = $element.data('isInViewport');
63+
const $element = $(this);
64+
const data = $element.data('isInViewport');
65+
6566
if (!data) {
66-
$element.data('isInViewport', (new Class(this, cb)));
67+
$element.data('isInViewport', (new IsInViewport(this, cb, offset)));
6768
}
6869
})
6970
}
7071

71-
}(window.jQuery);
72+
}(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.

gulpfile.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
var gulp = require('gulp');
2-
var minify = require('gulp-minify')
1+
'use strict';
2+
const gulp = require('gulp');
3+
const minify = require('gulp-minify');
34

4-
gulp.task('build', function () {
5-
gulp.src('isInViewport.jquery.js').pipe(minify({
6-
ext: {
7-
src: '.js',
8-
min: '.min.js',
9-
},
10-
exclude: ['tasks'],
11-
ignoreFiles: ['-min.js'],
12-
})).pipe(gulp.dest('dist'))
13-
})
5+
/**
6+
* Compiles SASS files.
7+
*/
8+
const jsTask = () => {
9+
return gulp.src('isInViewport.jquery.js')
10+
.pipe(minify({
11+
ext: {
12+
src: '.js',
13+
min: '.min.js',
14+
},
15+
exclude: ['tasks'],
16+
ignoreFiles: ['-min.js'],
17+
}))
18+
.pipe(gulp.dest('dist'))
19+
};
20+
21+
exports.build = jsTask;

isInViewport.jquery.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,72 @@
11
/* ====================================================
2-
* jQuery Is In Viewport.
2+
* jQuery is in viewport.
3+
*
34
* https://github.com/frontid/jQueryIsInViewport
45
* Marcelo Iván Tosco (capynet)
56
* Inspired on https://stackoverflow.com/a/40658647/1413049
67
* ==================================================== */
78
!function ($) {
8-
'use strict'
9+
'use strict';
910

10-
var Class = function (el, cb) {
11+
const IsInViewport = function (el, cb, offset) {
1112
this.$el = $(el);
1213
this.cb = cb;
14+
this.offset = offset;
1315
this.watch();
1416
return this;
1517
};
1618

17-
Class.prototype = {
19+
IsInViewport.prototype = {
1820

1921
/**
2022
* Checks if the element is in.
2123
*
2224
* @returns {boolean}
2325
*/
2426
isIn: function isIn() {
25-
var _self = this;
26-
var $win = $(window);
27-
var elementTop = _self.$el.offset().top;
28-
var elementBottom = elementTop + _self.$el.outerHeight();
29-
var viewportTop = $win.scrollTop();
30-
var viewportBottom = viewportTop + $win.height();
27+
const _self = this;
28+
const $win = $(window);
29+
const elementTop = _self.$el.offset().top - _self.offset;
30+
const elementBottom = elementTop + _self.$el.outerHeight();
31+
const viewportTop = $win.scrollTop();
32+
const viewportBottom = viewportTop + $win.height();
3133
return elementBottom > viewportTop && elementTop < viewportBottom;
3234
},
3335

3436
/**
3537
* Launch a callback indicating when the element is in and when is out.
3638
*/
3739
watch: function () {
38-
var _self = this;
39-
var _isIn = false;
40+
const self = this;
41+
let isIn = false;
4042

4143
$(window).on('resize scroll', function () {
4244

43-
if (_self.isIn() && _isIn === false) {
44-
_self.cb.call(_self.$el, 'entered');
45-
_isIn = true;
45+
if (self.isIn() && isIn === false) {
46+
self.cb.call(self.$el, 'entered');
47+
isIn = true;
4648
}
4749

48-
if (_isIn === true && !_self.isIn()) {
49-
_self.cb.call(_self.$el, 'leaved');
50-
_isIn = false;
50+
if (isIn === true && !self.isIn()) {
51+
self.cb.call(self.$el, 'leaved');
52+
isIn = false;
5153
}
52-
5354
})
5455
}
55-
56-
5756
};
5857

5958
// jQuery plugin.
6059
//-----------------------------------------------------------
61-
$.fn.isInViewport = function (cb) {
60+
$.fn.isInViewport = function (cb, offset) {
61+
offset || (offset = 0);
6262
return this.each(function () {
63-
var $element = $(this);
64-
var data = $element.data('isInViewport');
63+
const $element = $(this);
64+
const data = $element.data('isInViewport');
65+
6566
if (!data) {
66-
$element.data('isInViewport', (new Class(this, cb)));
67+
$element.data('isInViewport', (new IsInViewport(this, cb, offset)));
6768
}
6869
})
6970
}
7071

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

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
{
22
"name": "jquery-is-in-viewport",
3-
"version": "1.0.3",
4-
"description": "React when an element is in the viewport.",
3+
"version": "1.0.4",
4+
"scripts": {
5+
"build": "gulp build"
6+
},
7+
"description": "Reacts when an element is in the viewport.",
58
"main": "isInViewport.jquery.js",
69
"repository": "https://github.com/frontid/jQueryIsInViewport.git",
710
"author": "capynet <[email protected]>",
811
"license": "MIT",
912
"devDependencies": {
10-
"gulp": "*",
11-
"gulp-minify": "^1.0.0"
13+
"gulp": "^4.0.2",
14+
"gulp-minify": "^3.1.0"
1215
}
1316
}

0 commit comments

Comments
 (0)