-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.scrolling.js
157 lines (142 loc) · 4.19 KB
/
jquery.scrolling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* jquery.scrollIntoView
*
* Version: 0.1.20150317
*
* Copyright (c) 2015 Darkseal/Ryadel
* based on the work of Andrey Sidorov
* licensed under MIT license.
*
* Project Home Page:
* http://darkseal.github.io/jquery.scrolling/
*
* GitHub repository:
* https://github.com/darkseal/jquery.scrolling/
*
* Project Home Page on Ryadel.com:
* http://www.ryadel.com/
*
*/
(function ($) {
var selectors = [];
var checkBound = false;
var checkLock = false;
var extraOffsetTop = 0;
var extraOffsetLeft = 0;
var optionsAttribute = 'scrolling-options';
var defaults = {
interval: 250,
checkScrolling: false, // set it to "true" to perform an element "scroll-in" check immediately after startup
offsetTop: 0,
offsetLeft: 0,
offsetTopAttribute: 'offset-top',
offsetLeftAttribute: 'offset-left',
window: null // set a custom window object or leave it null to use current window.
// pass "top" to use the topmost frame.
}
var $window;
var $wasInView;
//https://stackoverflow.com/a/46078814
//JQuery 3.x didn't send selector
//$arsalanshah
$.fn._init = $.fn.init
$.fn.init = function( selector, context, root ) {
return (typeof selector === 'string') ? new $.fn._init(selector, context, root).data('selector', selector) : new $.fn._init( selector, context, root );
};
$.fn.getSelector = function() {
return $(this).data('selector');
};
function process() {
checkLock = false;
for (var index in selectors) {
var $inView = $(selectors.join(", ")).filter(function() {
return $(this).is(':scrollin');
});
}
$inView.trigger('scrollin', [$inView]);
if ($wasInView) {
var $notInView = $wasInView.not($inView);
$notInView.trigger('scrollout', [$notInView]);
}
$wasInView = $inView;
}
// "scrollin" custom filter
$.expr[':']['scrollin'] = function(element) {
var $element = $(element);
/* make it work with hidden pagination
if (!$element.is(':visible')) {
return false;
}
*/
if (!$element) {
return false;
}
var opts = $element.data(optionsAttribute);
if (typeof opts === 'undefined') {
return false;
}
var windowTop = $window.scrollTop();
var windowLeft = $window.scrollLeft();
var offset = $element.offset();
var top = offset.top + extraOffsetTop;
var left = offset.left + extraOffsetLeft;
if (top + $element.height() >= windowTop &&
top - ($element.data(opts.offsetTopAttribute) || opts.offsetTop) <= windowTop + $window.height() &&
left + $element.width() >= windowLeft &&
left - ($element.data(opts.offsetLeftAttribute) || opts.offsetLeft) <= windowLeft + $window.width()) {
return true;
} else {
return false;
}
}
$.fn.extend({
// watching for element's presence in browser viewport
scrolling: function(options) {
var opts = $.extend({}, defaults, options || {});
//Jquery < 3.0 shows a string as of $(<selector>) in 3.x it return JQuery object
//so to get a selector as of 2.x
//https://stackoverflow.com/a/46078814
// var selector = this.selector || this;
var selector = this.getSelector();
if(!selector){
return;
}
if (!checkBound) {
checkBound = true;
var onCheck = function() {
if (checkLock) {
return;
}
checkLock = true;
setTimeout(process, opts.interval);
};
$window = $(opts.window || window);
if ($window.get(0) != top) {
var $b = $($window.get(0).document.body);
if ($b) {
extraOffsetTop = $b.scrollTop();
extraOffsetLeft = $b.scrollLeft();
}
}
$window.scroll(onCheck).resize(onCheck);
}
var $el = $(selector);
$el.data(optionsAttribute, opts);
if (opts.checkScrolling) {
setTimeout(process, opts.interval);
}
selectors.push(selector);
return $el;
}
});
$.extend({
// force "scroll-in" check for the given element
checkScrolling: function() {
if (checkBound) {
process();
return true;
};
return false;
}
});
})(jQuery);