-
Notifications
You must be signed in to change notification settings - Fork 16
/
jquery.sticky-div.js
48 lines (43 loc) · 1.63 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
(function ($){
$.fn.sticky_div = function (options) {
var defaults = {
top: 0,
min_screen_width: 0,
bottom: 0,
outer_div: null
};
var options = $.extend(defaults, options);
// !important to override any current positioning or top.
$("<style type='text/css'> .sticky-div{ position:fixed !important; top:" + options.top + "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>");
}
});
// call to main method, so that it is not only a window scroll which triggers it
$.sticky_div(selector, options);
$(window).scroll(function () {
if ($(window).width() >= options.min_screen_width) {
// called again on scroll.
$.sticky_div(selector, options);
}
});
};
// main method, which does the magic
$.sticky_div = function (selector, options) {
var window_top = $(window).scrollTop();
var window_height = $(window).height();
selector.each(function () {
$(this).width($(this).width());
var div_top = $(this).prev().offset().top;
var div_height = $(this).outerHeight();
if ((!options.outer_div || (div_height < $(options.outer_div).height())) && (window_top > (div_top - options.top)) && (div_height < (window_height - options.bottom))) {
$(this).addClass('sticky-div');
} else {
$(this).removeClass('sticky-div');
}
});
}
})(jQuery);