forked from ymschaap/jquery-lockfixed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.lockfixed.js
90 lines (81 loc) · 3.05 KB
/
jquery.lockfixed.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
/*!
* jQuery lockfixed plugin
* http://www.directlyrics.com/code/lockfixed/
*
* Copyright 2012 Yvo Schaap
* Released under the MIT license
* http://www.directlyrics.com/code/lockfixed/license.txt
*
* Date: Sun Feb 9 2014 12:00:01 GMT
*/
(function($, undefined){
$.extend({
/**
* Lockfixed initiated
* @param {Element} el - a jquery element, DOM node or selector string
* @param {Object} config - offset - forcemargin
*/
"lockfixed": function(el, config){
if (config && config.offset) {
config.offset.bottom = parseInt(config.offset.bottom,10);
config.offset.top = parseInt(config.offset.top,10);
}else{
config.offset = {bottom: 100, top: 0};
}
var el = $(el);
if(el && el.offset()){
var el_position = el.css("position"),
el_margin_top = parseInt(el.css("marginTop"),10),
el_position_top = el.css("top"),
el_top = el.offset().top,
pos_not_fixed = false;
/*
* We prefer feature testing, too much hassle for the upside
* while prettier to use position: fixed (less jitter when scrolling)
* iOS 5+ + Android has fixed support, but issue with toggeling between fixed and not and zoomed view
*/
if (config.forcemargin === true || navigator.userAgent.match(/\bMSIE (4|5|6)\./) || navigator.userAgent.match(/\bOS ([0-9])_/) || navigator.userAgent.match(/\bAndroid ([0-9])\./i)){
pos_not_fixed = true;
}
/*
// adds throttle to scroll call
$(window).bind('scroll',el,function(e){
window.setTimeout(function(){
$(document).trigger('lockfixed:pageupdate');
},25);
});
*/
$(window).bind('scroll resize orientationchange load lockfixed:pageupdate',el,function(e){
// if we have a input focus don't change this (for smaller screens)
if(pos_not_fixed && document.activeElement && document.activeElement.nodeName === "INPUT"){
return;
}
var top = 0,
el_height = el.outerHeight(),
el_width = el.outerWidth(),
max_height = $(document).height() - config.offset.bottom,
scroll_top = $(window).scrollTop();
// if element is not currently fixed position, reset measurements ( this handles DOM changes in dynamic pages )
if (el.css("position") !== "fixed" && !pos_not_fixed) {
el_top = el.offset().top;
el_position_top = el.css("top");
}
if (scroll_top >= (el_top-(el_margin_top ? el_margin_top : 0)-config.offset.top)){
if(max_height < (scroll_top + el_height + el_margin_top + config.offset.top)){
top = (scroll_top + el_height + el_margin_top + config.offset.top) - max_height;
}else{
top = 0;
}
if (pos_not_fixed){
el.css({'marginTop': (parseInt(scroll_top - el_top - top,10) + (2 * config.offset.top))+'px'});
}else{
el.css({'position': 'fixed','top':(config.offset.top-top)+'px','width':el_width +"px"});
}
}else{
el.css({'position': el_position,'top': el_position_top, 'width':el_width +"px", 'marginTop': (el_margin_top && !pos_not_fixed ? el_margin_top : 0)+"px"});
}
});
}
}
});
})(jQuery);