forked from webpop/jquery.pin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.pin.js
147 lines (125 loc) · 3.7 KB
/
jquery.pin.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
(function($) {
"use strict";
$.fn.pin = function(options) {
var scrollY = 0,
elements = [],
disabled = false,
$window = $(window);
options = options || {};
var recalculateLimits = function() {
for (var i = 0, len = elements.length; i < len; i++) {
var $this = elements[i];
if (options.minWidth && $window.width() <= options.minWidth) {
if ($this.parent().is(".pin-wrapper")) {
$this.unwrap();
}
$this.css({
width: "",
left: "",
top: "",
position: ""
});
if (options.activeClass) {
$this.removeClass(options.activeClass);
}
disabled = true;
continue;
} else {
disabled = false;
}
var $container = options.containerSelector ? $this.closest(options.containerSelector) : $(document.body);
var offset = $this.offset();
var containerOffset = $container.offset();
var parentOffset = $this.offsetParent().offset();
if (!$this.parent().is(".pin-wrapper")) {
$this.wrap("<div class='pin-wrapper'>");
}
var pad = $.extend({
top: 0,
bottom: 0
}, options.padding || {});
$this.data("pin", {
pad: pad,
from: (options.containerSelector ? containerOffset.top : offset.top) - pad.top,
to: containerOffset.top + $container.height() - $this.outerHeight() - pad.bottom,
end: containerOffset.top + $container.height(),
parentTop: parentOffset.top
});
$this.css({
width: $this.outerWidth()
});
$this.parent().css("height", $this.outerHeight());
}
};
var onScroll = function() {
if (disabled) {
return;
}
scrollY = $window.scrollTop();
var elmts = [];
for (var i = 0, len = elements.length; i < len; i++) {
var $this = $(elements[i]),
data = $this.data("pin");
if (!data) { // Removed element
continue;
}
elmts.push($this);
var from = data.from - data.pad.bottom,
to = data.to - data.pad.top;
if (from + $this.outerHeight() > data.end) {
$this.css('position', '');
continue;
}
if (from < scrollY && to > scrollY) {
!($this.css("position") == "fixed") && $this.css({
left: $this.offset().left,
top: data.pad.top
}).css("position", "fixed");
if (options.activeClass) {
$this.addClass(options.activeClass);
}
} else if (scrollY >= to) {
$this.css({
left: "",
top: to - data.parentTop + data.pad.top
}).css("position", "absolute");
if (options.activeClass) {
$this.addClass(options.activeClass);
}
} else {
$this.css({
position: "",
top: "",
left: ""
});
if (options.activeClass) {
$this.removeClass(options.activeClass);
}
}
}
elements = elmts;
};
var update = function() {
recalculateLimits();
onScroll();
};
this.each(function() {
var $this = $(this),
data = $(this).data('pin') || {};
if (data && data.update) {
return;
}
elements.push($this);
$("img", this).one("load", recalculateLimits);
data.update = update;
$(this).data('pin', data);
});
$window.scroll(onScroll);
$window.resize(function() {
recalculateLimits();
});
recalculateLimits();
$window.load(update);
return this;
};
})(jQuery);