-
Notifications
You must be signed in to change notification settings - Fork 54
/
bottom-sticker.js
63 lines (52 loc) · 1.18 KB
/
bottom-sticker.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
/*!
* bootm sticker
* Copyright 2016-2020 Pactear.
* Licensed under the MIT license
* @author [email protected]
* @date 2016-4-8
* @version 0.0.1
*/
(function($) {
$.fn.bottomSticker = function (options) {
var config = {
bottom: '0px',
width: '100%'
};
if (options){ $.extend(config, options); }
return this.each( function() {
var o = $(this)
, cId = o.attr("id") + "-copy";
var c = "." + cId;
var $win = $(window)
, isFixed = 0;
function processScroll() {
var scrollTop = $win.scrollTop()
, cTop = $(c).offset().top
, oTop = o.offset().top;
if (oTop > cTop && !isFixed) {
isFixed = 1;
} else if (oTop <= cTop && isFixed) {
isFixed = 0;
}
isFixed ? $(c).show().width(getWidth())
: $(c).hide();
}
function getWidth() {
var width = o.width();
if (width == 0)
width = config['width'];
return width;
}
$win.on('scroll', processScroll);
$win.on('resize', processScroll);
var clone = o.clone(true)
.attr("id", cId)
.addClass(cId)
.css({'position': 'fixed'
, 'bottom': config['bottom']
, 'width': getWidth()});
o.after(clone);
processScroll();
}); // return each
};
})(jQuery);