forked from 1337/jquery_viewport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.viewport.js
169 lines (149 loc) · 5.77 KB
/
jquery.viewport.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
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Viewport - jQuery selectors for finding elements in viewport
*
* Copyright (c) 2008-2009 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* http://www.appelsiini.net/projects/viewport
*
*/
/*global define,require*/
(function (root, factory) {
"use strict";
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
factory(require('jquery'));
} else {
factory(root.jQuery);
}
}(this, function ($) {
"use strict";
var $window = $(window);
function int(val) {
return parseInt(val, 10) || 0;
}
function belowTheFold(element, settings) {
var fold = $window.height() + $window.scrollTop();
return fold <= Math.round($(element).offset().top) - settings.threshold;
}
function belowTheFoldCompletely(element, settings) {
var $element = $(element),
fold = $window.height() + $window.scrollTop();
return fold <= $element.offset().top + $element.height() - settings.threshold;
}
function aboveTheTop(element, settings) {
var $element = $(element),
top = $window.scrollTop();
return top >= Math.round($element.offset().top) +
$element.height() - settings.threshold;
}
function aboveTheTopCompletely(element, settings) {
var top = $window.scrollTop();
return top >= $(element).offset().top - settings.threshold;
}
function rightOfScreen(element, settings) {
var fold = $window.width() + $window.scrollLeft();
return fold <= $(element).offset().left - settings.threshold;
}
function rightOfScreenCompletely(element, settings) {
var $element = $(element),
fold = $window.width() + $window.scrollLeft();
return fold <= $element.offset().left + $element.width() - settings.threshold;
}
function leftOfScreen(element, settings) {
var $element = $(element),
left = $window.scrollLeft();
return left >= Math.round($element.offset().left) + $element.width() - settings.threshold;
}
function leftOfScreenCompletely(element, settings) {
var left = $window.scrollLeft();
return left >= $(element).offset().left - settings.threshold;
}
function inViewport(element, settings) {
var $element = $(element),
offset = $element.offset();
// Return false if element is hidden.
if (!$element.is(':visible')) {
return false;
}
var windowTop = $window.scrollTop(),
threshold = settings.threshold;
if (offset.top - threshold < windowTop) {
if (offset.top + $element.height() + threshold >= windowTop) {
// top edge below the window's top
} else {
return false;
}
} else if (offset.top - threshold > windowTop + $window.height()) {
// not (bottom edge above the window's bottom)
return false;
}
var windowLeft = $window.scrollLeft();
if (offset.left - threshold < windowLeft) {
if (offset.left + $element.width() + threshold >= windowLeft) {
// left edge be on the left side of the window's left edge
} else {
return false;
}
} else if (offset.left - threshold > windowLeft + $window.width()) {
// not (right edge be on the right side of the window's right edge)
return false;
}
return true;
}
$.extend($.expr[':'], {
"below-the-fold": function (a, i, m) {
// m[3] is supposedly the threshold (@theluk)
return belowTheFold(a, {threshold: int(m[3])});
},
"below-the-fold-completely": function (a, i, m) {
// m[3] is supposedly the threshold (@theluk)
return belowTheFoldCompletely(a, {threshold: int(m[3])});
},
"above-the-top": function (a, i, m) {
// m[3] is supposedly the threshold (@theluk)
return aboveTheTop(a, {threshold: int(m[3])});
},
"above-the-top-completely": function (a, i, m) {
// m[3] is supposedly the threshold (@theluk)
return aboveTheTopCompletely(a, {threshold: int(m[3])});
},
"left-of-screen": function (a, i, m) {
// m[3] is supposedly the threshold (@theluk)
return leftOfScreen(a, {threshold: int(m[3])});
},
"left-of-screen-completely": function (a, i, m) {
// m[3] is supposedly the threshold (@theluk)
return leftOfScreenCompletely(a, {threshold: int(m[3])});
},
"right-of-screen": function (a, i, m) {
// m[3] is supposedly the threshold (@theluk)
return rightOfScreen(a, {threshold: int(m[3])});
},
"right-of-screen-completely": function (a, i, m) {
// m[3] is supposedly the threshold (@theluk)
return rightOfScreenCompletely(a, {threshold: int(m[3])});
},
"in-viewport": function (a, i, m) {
// m[3] is supposedly the threshold (@theluk)
return inViewport(a, {threshold: int(m[3])});
}
});
// Export some functions for testing.
$.viewport = $.viewport || {};
$.extend($.viewport, {
aboveTheTop: aboveTheTop,
aboveTheTopCompletely: aboveTheTopCompletely,
belowTheFold: belowTheFold,
belowTheFoldCompletely: belowTheFoldCompletely,
inViewport: inViewport,
leftOfScreen: leftOfScreen,
leftOfScreenCompletely: leftOfScreenCompletely,
rightOfScreen: rightOfScreen,
rightOfScreenCompletely: rightOfScreenCompletely
});
}));