-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfixedElemScroll.jquery.js
62 lines (54 loc) · 2.25 KB
/
fixedElemScroll.jquery.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
// jQuery fixedElemScroll Plugin
// version 0.1, Feb. 17th, 2012
// by Mikhail Sabodzhan and Stanislas Duprey
(function($) {
$.fn.fixedElemScroll = function(options) {
// public methods to be called from outside the plugin
var methods = {}
var $this = $(this),
$window = $(window),
settings,
process;
// if options is a string then it's trying to call a public method
if (typeof options === "string") return methods[options].apply(this, Array.prototype.slice.call(arguments, 1));
settings = $.extend({
$parent: $this.parent(),
// if the parent's element position is static, bottom will be the difference between the bottom of the parent element
// and the bottom of the closest parent element with a position not equal "static"
bottom: 0
}, options);
process = function() {
// fix for IE7 layout bug
settings.$parent.css({'zoom':1});
function stickIt() {
// check if the bottom of the parent element is inside the view port
if (settings.$parent.offset().top + settings.$parent.height() - settings.bottom < $window.scrollTop() + $window.height()) {
$this.css({
position: "absolute",
bottom: settings.bottom
});
} else {
// check if the top of the parent element is outside the view port
if (($this.offset().top + $this.height()) < $window.scrollTop() + $window.height() || $this.css("position") === "absolute") {
$this.css({
position: "fixed",
bottom: 0
});
}
// check if the image is higher then the parent element
if (settings.$parent.offset().top >= $this.offset().top) {
$this.css({ position: "relative" });
}
}
};
stickIt();
$window.scroll(function() { stickIt(); });
$window.resize(function() { stickIt(); });
};
return this.each(function() {
if (settings.$parent.css("position") === "static") { settings.$parent.css("position", "relative"); }
if ($this.height()>0) process();
else $this.load(process);
});
}
})(jQuery);