forked from dubroe/sticky-div
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.sticky-div.js
87 lines (79 loc) · 3 KB
/
jquery.sticky-div.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
(function ($){
$.fn.sticky_div = function (options) {
var defaults = {
top: 0,
min_screen_width: 0,
min_screen_height: 0,
bottom: 0,
stick_bottom: false,
outer_div: null,
style_css: null
};
var options = $.extend(defaults, options);
// !important to override any current positioning or top/bottom.
$("<style type='text/css'>"
+ " .sticky-div-top { position: fixed !important; top: " + options.top + "px !important; }"
+ " .sticky-div-bottom { position: fixed !important; bottom: " + options.bottom + "px !important; }"
+ " </style>")
.appendTo("head");
var selector = this;
// add a class to the anchor, and check for it, so that the anchor <div/> is only added the once.
selector.each(function () {
if (!$(this).prev().hasClass("sticky-anchor")) {
$(this).before("<div class='sticky-anchor'></div>");
}
});
var call_sticky_div = function() {
$.sticky_div(selector, options);
};
// call main method when DOM is ready
$(document).ready(call_sticky_div);
// called again on scroll
$(window).scroll(call_sticky_div);
// called again on resize
$(window).resize(call_sticky_div);
};
// main method, which does the magic
$.sticky_div = function (selector, options) {
var window_top = $(window).scrollTop();
var window_width = $(window).width();
var window_height = $(window).height();
selector.each(function () {
$(this).width($(this).width());
var anchor = $(this).prev();
var div_top = anchor.offset().top;
var div_height = $(this).outerHeight();
var bool_sticky_div = (!options.outer_div || (div_height < $(options.outer_div).height()));
bool_sticky_div = bool_sticky_div && (window_width >= options.min_screen_width);
bool_sticky_div = bool_sticky_div && (window_height >= options.min_screen_height);
if (options.stick_bottom) {
bool_sticky_div = bool_sticky_div && ((window_top + window_height) < ((div_top + div_height) + options.bottom));
bool_sticky_div = bool_sticky_div && (div_height < (window_height - options.top));
} else {
bool_sticky_div = bool_sticky_div && (window_top > (div_top - options.top));
bool_sticky_div = bool_sticky_div && (div_height < (window_height - options.bottom));
}
if (bool_sticky_div) {
if (options.stick_bottom) {
$(this).addClass('sticky-div-bottom');
} else {
$(this).addClass('sticky-div-top');
}
if (options.style_css != null) {
$(this).addClass(options.style_css);
}
anchor.height(div_height);
} else {
if (options.stick_bottom) {
$(this).removeClass('sticky-div-bottom');
} else {
$(this).removeClass('sticky-div-top');
}
if (options.style_css != null) {
$(this).removeClass(options.style_css);
}
anchor.height(0);
}
});
};
})(jQuery);