Skip to content

Commit 2652d68

Browse files
author
capynet
committed
Add support for bower, yarn and npm
1 parent 311c64a commit 2652d68

File tree

7 files changed

+122
-0
lines changed

7 files changed

+122
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
node_modules

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
This plugin allows you to react when an element is in the viewport.
44
You need throw confetti when the user scroll and see an element?, so this plugin is for you!
55

6+
## Install
7+
```bash
8+
npm install jquery-is-in-viewport --save
9+
// or
10+
yarn add jquery-is-in-viewport
11+
// or
12+
bower install jquery-is-in-viewport
13+
```
14+
615
## Usage
716
```javascript
817
$('.block').isInViewport(function (status) {

bower.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "jquery-is-in-viewport",
3+
"homepage": "https://github.com/frontid/jQueryIsInViewport",
4+
"authors": [
5+
"capynet <[email protected]>"
6+
],
7+
"description": "React when an element is in the viewport.",
8+
"main": "isInViewport.jquery.js",
9+
"license": "MIT",
10+
"ignore": [
11+
"**/.*",
12+
"node_modules",
13+
"bower_components",
14+
"test",
15+
"tests"
16+
]
17+
}

dist/isInViewport.jquery.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* ====================================================
2+
* jQuery Is In Viewport.
3+
* https://github.com/frontid/jQueryIsInViewport
4+
* Marcelo Iván Tosco (capynet)
5+
* Inspired on https://stackoverflow.com/a/40658647/1413049
6+
* ==================================================== */
7+
!function ($) {
8+
'use strict'
9+
10+
var plugin
11+
12+
var Class = function (el, cb) {
13+
plugin = this
14+
this.$el = $(el)
15+
this.cb = cb
16+
watch()
17+
return this
18+
}
19+
20+
/**
21+
* Checks if the element is in.
22+
*
23+
* @returns {boolean}
24+
*/
25+
function isIn () {
26+
var $win = $(window)
27+
var elementTop = plugin.$el.offset().top
28+
var elementBottom = elementTop + plugin.$el.outerHeight()
29+
var viewportTop = $win.scrollTop()
30+
var viewportBottom = viewportTop + $win.height()
31+
return elementBottom > viewportTop && elementTop < viewportBottom
32+
}
33+
34+
/**
35+
* Launch a callback indicating when the element is in and when is out.
36+
*/
37+
function watch () {
38+
var _isIn = false
39+
40+
$(window).on('resize scroll', function () {
41+
42+
if (isIn() && _isIn === false) {
43+
plugin.cb('entered')
44+
_isIn = true
45+
}
46+
47+
if (_isIn === true && !isIn()) {
48+
plugin.cb('leaved')
49+
_isIn = false
50+
}
51+
52+
})
53+
}
54+
55+
// jQuery plugin.
56+
//-----------------------------------------------------------
57+
$.fn.isInViewport = function (cb) {
58+
return this.each(function () {
59+
var $element = $(this)
60+
var data = $element.data('isInViewport')
61+
if (!data) {
62+
$element.data('isInViewport', (new Class(this, cb)))
63+
}
64+
})
65+
}
66+
67+
}(window.jQuery)

dist/isInViewport.jquery.min.js

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

gulpfile.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var gulp = require('gulp');
2+
var minify = require('gulp-minify')
3+
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+
})

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "jquery-is-in-viewport",
3+
"version": "1.0.0",
4+
"description": "React when an element is in the viewport.",
5+
"main": "isInViewport.jquery.js",
6+
"repository": "https://github.com/frontid/jQueryIsInViewport.git",
7+
"author": "capynet <[email protected]>",
8+
"license": "MIT",
9+
"devDependencies": {
10+
"gulp": "*",
11+
"gulp-minify": "^1.0.0"
12+
}
13+
}

0 commit comments

Comments
 (0)