forked from patik/within-viewport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.withinViewport.js
109 lines (99 loc) · 3.01 KB
/
jquery.withinViewport.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
/**
* Within Viewport jQuery Plugin
*
* @description Companion plugin for withinViewport.js
* @author Craig Patik, http://patik.com/
* @version 0.2
* @date 2011-11-05
*/
;(function( $, window, undefined ) {
/**
* $.withinViewport()
* @description jQuery method
* @param {Object} [settings] optional settings
* @return {Collection} Contains all elements that were within the viewport
*/
$.fn.withinViewport = function(settings) {
if (typeof settings === "string") { settings = {sides: settings}; }
var opts = $.extend({}, settings, {sides: "all"}), elems = [];
this.each(function() {
if (withinViewport(this, opts)) {
elems.push(this);
}
});
return $(elems);
};
// Custom selector
$.extend($.expr[":"], {
"within-viewport": function(element) {
return withinViewport(element, "all");
}
});
/**
* Optional enhancements and shortcuts
*
* @description Uncomment or comment these pieces as they apply to your project and coding preferences
*/
// Shorthand jQuery methods
//
$.fn.withinViewportTop = function(settings) {
if (typeof settings === "string") { settings = {sides: settings}; }
var opts = $.extend({}, settings, {sides: "top"}), elems = [];
this.each(function() {
if (withinViewport(this, opts)) {
elems.push(this);
}
});
return $(elems);
};
$.fn.withinViewportRight = function(settings) {
if (typeof settings === "string") { settings = {sides: settings}; }
var opts = $.extend({}, settings, {sides: "right"}), elems = [];
this.each(function() {
if (withinViewport(this, opts)) {
elems.push(this);
}
});
return $(elems);
};
$.fn.withinViewportBottom = function(settings) {
if (typeof settings === "string") { settings = {sides: settings}; }
var opts = $.extend({}, settings, {sides: "bottom"}), elems = [];
this.each(function() {
if (withinViewport(this, opts)) {
elems.push(this);
}
});
return $(elems);
};
$.fn.withinViewportLeft = function(settings) {
if (typeof settings === "string") { settings = {sides: settings}; }
var opts = $.extend({}, settings, {sides: "left"}), elems = [];
this.each(function() {
if (withinViewport(this, opts)) {
elems.push(this);
}
});
return $(elems);
};
// Custom jQuery selectors
//
$.extend($.expr[":"], {
"within-viewport-top": function(element) {
return withinViewport(element, "top");
},
"within-viewport-right": function(element) {
return withinViewport(element, "right");
},
"within-viewport-bottom": function(element) {
return withinViewport(element, "bottom");
},
"within-viewport-left": function(element) {
return withinViewport(element, "left");
}
//,
// "within-viewport-top-left-45": function(element) {
// return withinViewport(element, {sides:'top left', top: 45, left: 45});
// }
});
})(jQuery, window);