|
7 | 7 | !function ($) {
|
8 | 8 | 'use strict'
|
9 | 9 |
|
10 |
| - var plugin |
11 |
| - |
12 | 10 | var Class = function (el, cb) {
|
13 |
| - plugin = this |
14 |
| - this.$el = $(el) |
15 |
| - this.cb = cb |
16 |
| - watch() |
17 |
| - return this |
18 |
| - } |
| 11 | + this.$el = $(el); |
| 12 | + this.cb = cb; |
| 13 | + this.watch(); |
| 14 | + return this; |
| 15 | + }; |
19 | 16 |
|
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 |
| - } |
| 17 | + Class.prototype = { |
33 | 18 |
|
34 |
| - /** |
35 |
| - * Launch a callback indicating when the element is in and when is out. |
36 |
| - */ |
37 |
| - function watch () { |
38 |
| - var _isIn = false |
| 19 | + /** |
| 20 | + * Checks if the element is in. |
| 21 | + * |
| 22 | + * @returns {boolean} |
| 23 | + */ |
| 24 | + 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(); |
| 31 | + return elementBottom > viewportTop && elementTop < viewportBottom; |
| 32 | + }, |
39 | 33 |
|
40 |
| - $(window).on('resize scroll', function () { |
| 34 | + /** |
| 35 | + * Launch a callback indicating when the element is in and when is out. |
| 36 | + */ |
| 37 | + watch: function () { |
| 38 | + var _self = this; |
| 39 | + var _isIn = false; |
41 | 40 |
|
42 |
| - if (isIn() && _isIn === false) { |
43 |
| - plugin.cb.call(plugin.$el, 'entered') |
44 |
| - _isIn = true |
45 |
| - } |
| 41 | + $(window).on('resize scroll', function () { |
46 | 42 |
|
47 |
| - if (_isIn === true && !isIn()) { |
48 |
| - plugin.cb.call(plugin.$el, 'leaved') |
49 |
| - _isIn = false |
50 |
| - } |
| 43 | + if (_self.isIn() && _isIn === false) { |
| 44 | + _self.cb.call(_self.$el, 'entered'); |
| 45 | + _isIn = true; |
| 46 | + } |
51 | 47 |
|
52 |
| - }) |
53 |
| - } |
| 48 | + if (_isIn === true && !_self.isIn()) { |
| 49 | + _self.cb.call(_self.$el, 'leaved'); |
| 50 | + _isIn = false; |
| 51 | + } |
| 52 | + |
| 53 | + }) |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + }; |
54 | 58 |
|
55 | 59 | // jQuery plugin.
|
56 | 60 | //-----------------------------------------------------------
|
57 | 61 | $.fn.isInViewport = function (cb) {
|
58 | 62 | return this.each(function () {
|
59 |
| - var $element = $(this) |
60 |
| - var data = $element.data('isInViewport') |
| 63 | + var $element = $(this); |
| 64 | + var data = $element.data('isInViewport'); |
61 | 65 | if (!data) {
|
62 |
| - $element.data('isInViewport', (new Class(this, cb))) |
| 66 | + $element.data('isInViewport', (new Class(this, cb))); |
63 | 67 | }
|
64 | 68 | })
|
65 | 69 | }
|
66 | 70 |
|
67 |
| -}(window.jQuery) |
| 71 | +}(window.jQuery); |
0 commit comments