5 * Utility library for viewport-related functions
8 * - https://github.com/tuupola/jquery_lazyload
9 * - https://github.com/luis-almeida/unveil
17 * This is a private method pulled inside the module for testing purposes.
21 * @return {Object} Viewport positions
23 makeViewportFromWindow: function () {
24 var $window
= $( window
),
25 scrollTop
= $window
.scrollTop(),
26 scrollLeft
= $window
.scrollLeft();
31 right
: scrollLeft
+ $window
.width(),
32 bottom
: ( window
.innerHeight
? window
.innerHeight
: $window
.height() ) + scrollTop
37 * Check if any part of a given element is in a given viewport
40 * @param {HTMLElement} el Element that's being tested
41 * @param {Object} [rectangle] Viewport to test against; structured as such:
49 * Defaults to viewport made from `window`.
53 isElementInViewport: function ( el
, rectangle
) {
55 offset
= $el
.offset(),
62 viewport
= rectangle
|| this.makeViewportFromWindow();
65 // Top border must be above viewport's bottom
66 ( viewport
.bottom
>= rect
.top
) &&
67 // Left border must be before viewport's right border
68 ( viewport
.right
>= rect
.left
) &&
69 // Bottom border must be below viewport's top
70 ( viewport
.top
<= rect
.top
+ rect
.height
) &&
71 // Right border must be after viewport's left border
72 ( viewport
.left
<= rect
.left
+ rect
.width
)
77 * Check if an element is a given threshold away in any direction from a given viewport
80 * @param {HTMLElement} el Element that's being tested
81 * @param {number} [threshold] Pixel distance considered "close". Must be a positive number.
83 * @param {Object} [rectangle] Viewport to test against.
84 * Defaults to viewport made from `window`.
87 isElementCloseToViewport: function ( el
, threshold
, rectangle
) {
88 var viewport
= rectangle
? $.extend( {}, rectangle
) : this.makeViewportFromWindow();
89 threshold
= threshold
|| 50;
91 viewport
.top
-= threshold
;
92 viewport
.left
-= threshold
;
93 viewport
.right
+= threshold
;
94 viewport
.bottom
+= threshold
;
95 return this.isElementInViewport( el
, viewport
);
100 mw
.viewport
= viewport
;
101 }( mediaWiki
, jQuery
) );