-
Notifications
You must be signed in to change notification settings - Fork 0
/
floatBlock.js
88 lines (83 loc) · 2.75 KB
/
floatBlock.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
/*
Usage:
new FloatBlock({
block: '#floatNavBlock',
topLimitBlock: '[data-floatBlock="topLimit"]',
bottomLimitBlock: '[data-floatBlock="bottomLimit"]',
aligningBlock: '[data-floatBlock="topLimit"]'
});
*/
var FloatBlock = (function($, global){
var defaultOptions = {},
init,
bindEvents,
global = $(global),
windowScrollTop,
windowHeight = 0,
updateDimensions,
checkState,
module;
init = function(options) {
this.options = $.extend({}, defaultOptions, options);
this.block = $(options.block);
this.blockHeight = this.block.outerHeight();
this.topLimitBlock = $(options.topLimitBlock);
this.bottomLimitBlock = $(options.bottomLimitBlock);
this.aligningBlock = $(options.aligningBlock);
this.block.appendTo('body');
updateDimensions.call(this);
checkState.call(this);
bindEvents.call(this);
};
bindEvents = function() {
var checkTimer = 0;
global.on('resize.floatBlock', $.proxy(function(){
clearTimeout(checkTimer);
checkTimer = setTimeout($.proxy(function(){
updateDimensions.call(this);
checkState.call(this);
}, this), 100);
}, this));
global.on('scroll.floatBlock', $.proxy(function(){
updateDimensions.call(this);
checkState.call(this);
}, this));
};
updateDimensions = function(){
this.topLimitPoint = this.topLimitBlock.offset().top + this.topLimitBlock.outerHeight();
this.bottomLimitPoint = this.bottomLimitBlock.offset().top;
this.left = (global.scrollLeft() - this.aligningBlock.offset().left) * - 1;
windowScrollTop = global.scrollTop();
windowHeight = global.height();
};
checkState = function() {
if (windowScrollTop + this.blockHeight >= this.bottomLimitPoint) {
this.currentState = 'top';
this.block.css({
position: 'absolute',
top: this.bottomLimitPoint - this.blockHeight,
left: this.aligningBlock.offset().left
});
return;
} else if (windowScrollTop >= this.topLimitPoint) {
this.currentState = 'float';
this.block.css({
position: 'fixed',
top: 0,
left: this.left
});
return;
} else {
this.currentState = 'bottom';
this.block.css({
position: 'absolute',
top: this.topLimitPoint,
left: this.aligningBlock.offset().left
});
}
};
module = function() {
init.apply(this, arguments);
};
return module;
})(jQuery, window);