-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfixedTop.js
54 lines (53 loc) · 2.27 KB
/
fixedTop.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
(function ($) {
$.extend($.fn, {
fixedTop: function (options) {
var opt = {
top: 0, //距离屏幕的上边距
zIndex: 0,
bottom:0 //距离滚动页面最尾部距离,取消固定位置
}
opt = $.extend(opt, options);
this.each(function () {
var self = this,
top = self.offsetTop,
seftPos = self.getBoundingClientRect(),
left=seftPos.left+(document.documentElement.scrollLeft || document.body.scrollLeft),
selfHeight = seftPos.height,
selfWidth = seftPos.width;
setInterval(function () {
var st = document.documentElement.scrollTop || document.body.scrollTop,
sl=document.documentElement.scrollLeft || document.body.scrollLeft,
msl=$(document).width()-document.documentElement.clientWidth,
pos = 0,
height = 0;
if (!pos && st - top + opt.top >= 0) {
$(self).css({
'position': 'fixed',
'top': 0,
'left':sl>0?left-sl:'auto',
'padding-top': opt.top,
'width': selfWidth,
'z-index': opt.zIndex
});
pos = 1;
if (document.documentElement.clientHeight - opt.bottom < selfHeight) {
height = $(document).height();
if (st + selfHeight + 200 > height) {
$(self).css({
'position': 'static',
'padding-top': 0
});
}
}
}
else if (st - top <= 0) {
$(self).css({
'position': 'static',
'padding-top': 0
});
}
}, 16);
})
}
});
})($ || jQuery || Zepto);