-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjquery.stickyslider.js
More file actions
75 lines (61 loc) · 2.54 KB
/
jquery.stickyslider.js
File metadata and controls
75 lines (61 loc) · 2.54 KB
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
/*
Copyright (c) 2012 Patryk Kasperski
Dual licensed under the MIT and GPL licenses.
jQuery StickySlider
Author: Patryk Kasperski
Version: 0.2.1
*/
;(function($){
$.fn.stickySlider = function( options ) {
var defaults = {
stopAtElement: null,
parentContainer: false,
contentWrapper: false,
footerBottomMargin: 10
};
// extend the options from defaults with user's options
var opts = $.extend({}, defaults, options);
if(opts.footer.length == 0) return;
return this.each(function(){
var $this = $(this);
var $footer = opts.stopAtElement;
$(window).bind("resize", $.throttle(10, function() {
$(window).scroll();
}));
if(opts.parentContainer && opts.contentWrapper) {
if(opts.parentContainer.height() > opts.contentWrapper.height())
return;
}
opts.contentWrapper.css({position: 'relative'});
if ($this.height() < $(window).height() && $(window).scrollLeft() == 0) {
stickNavigation();
}
function stickNavigation() {
var headerHeight = $this.offset().top;
var footerPosition = $footer.offset().top;
var lastItemPosition = $this.offset().top + ($this.outerHeight() + 30);
$(window).bind("scroll", $.throttle(0, function() {
footerPosition = $footer.offset().top - 30;
lastItemPosition = parseInt($this.offset().top + ($this.height()));
setNavPosition({
headerHeight: headerHeight,
footerPosition: footerPosition,
lastItemPosition: lastItemPosition
});
}));
}
function setNavPosition(elValues) {
if (elValues.lastItemPosition < elValues.footerPosition) {
$this.removeClass('stuck-bottom');
$this.toggleClass('stuck', $(document).scrollTop() > (elValues.headerHeight)); // true=add; false=remove
} else {
if($this.hasClass('stuck-bottom') && $this.offset().top > $(document).scrollTop()) {
$this.removeClass('stuck-bottom').addClass('stuck');
} else {
$this.removeClass('stuck').addClass('stuck-bottom');
}
}
}
});
};
})(jQuery);