-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.stick.js
83 lines (72 loc) · 1.91 KB
/
jquery.stick.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
(function() {
var $, FIXED, STATIC, Stick;
$ = jQuery;
STATIC = 'static';
FIXED = 'fixed';
$.fn.stick = function(args) {
var s;
return s = new Stick(this, args);
};
Stick = (function() {
function Stick(target, args) {
this.target = target;
this.offset = typeof args === "number" ? args : 0;
if (typeof args === "object") {
$.extend(this, args);
}
this.state = STATIC;
this.resize();
(function(s) {
return $(window).scroll(function() {
return s.scroll(this);
}).resize(function() {
return s.resize(this);
});
})(this);
}
Stick.prototype.position = function() {
return this.target.css({
"position": this.state,
"top": this.offset + "px",
"left": this.targetLeft + "px"
});
};
Stick.prototype.resize = function() {
var targetBottom;
this.targetOffset = parseInt(this.target.offset().top);
this.targetLeft = parseInt(this.target.offset().left);
targetBottom = this.targetOffset + this.target.height();
if ($(window).height() < targetBottom) {
this.disabled = true;
if (this.state === FIXED) {
return this.position();
}
} else {
this.disabled = false;
return this.scroll();
}
};
Stick.prototype.scroll = function(self) {
if (this.disabled) {
return;
}
if ((this.targetOffset - $(self).scrollTop()) - this.offset <= 0) {
if (this.state === STATIC) {
if (typeof this.onStick === "function") {
this.onStick();
}
}
this.state = FIXED;
} else {
if (this.state === FIXED) {
if (typeof this.onUnstick === "function") {
this.onUnstick();
}
}
this.state = STATIC;
}
return this.position();
};
return Stick;
})();
})();