|
@@ -0,0 +1,153 @@
|
|
1
|
+/*jslint browser:true */
|
|
2
|
+/*global jQuery*/
|
|
3
|
+
|
|
4
|
+(function ($, window, undefined) {
|
|
5
|
+ 'use strict';
|
|
6
|
+
|
|
7
|
+ var pluginName = 'stickySidebar',
|
|
8
|
+ document = window.document,
|
|
9
|
+ defaults = {
|
|
10
|
+ sidebar: '.sidebar',
|
|
11
|
+ content: '.content',
|
|
12
|
+ tolerance: 110
|
|
13
|
+ },
|
|
14
|
+ clearPosition = {
|
|
15
|
+ position: '',
|
|
16
|
+ top: ''
|
|
17
|
+ };
|
|
18
|
+
|
|
19
|
+ function Plugin(element, options) {
|
|
20
|
+ this.options = $.extend({}, defaults, options);
|
|
21
|
+
|
|
22
|
+ this.defaults = defaults;
|
|
23
|
+ this.name = pluginName;
|
|
24
|
+
|
|
25
|
+ this.root = $(element);
|
|
26
|
+
|
|
27
|
+ this.init();
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ Plugin.prototype.init = function () {
|
|
31
|
+ var self = this;
|
|
32
|
+
|
|
33
|
+ this.sidebar = this.root.find(this.options.sidebar);
|
|
34
|
+ this.content = this.root.find(this.options.content);
|
|
35
|
+ this.lastScrollTop = 0;
|
|
36
|
+
|
|
37
|
+ if (this.canBeSticky() === true) {
|
|
38
|
+ $(window).bind('scroll.sticky_sidebar', function () {
|
|
39
|
+ self.setScrollDirection()
|
|
40
|
+ .setBoundaries()
|
|
41
|
+ .positionSidebar();
|
|
42
|
+ });
|
|
43
|
+ }
|
|
44
|
+ };
|
|
45
|
+
|
|
46
|
+ Plugin.prototype.canBeSticky = function () {
|
|
47
|
+ return this.sidebar.height() < this.content.height() &&
|
|
48
|
+ this.sidebar.height() > $(window).height();
|
|
49
|
+ };
|
|
50
|
+
|
|
51
|
+ Plugin.prototype.setBoundaries = function () {
|
|
52
|
+ var contentTop = this.content.offset().top,
|
|
53
|
+ contentHeight = this.content.outerHeight();
|
|
54
|
+ this.boundaries = {
|
|
55
|
+ contentTop: contentTop,
|
|
56
|
+ contentBottom: contentTop + contentHeight,
|
|
57
|
+ contentHeight: contentHeight,
|
|
58
|
+ sidebarHeight: this.sidebar.outerHeight(),
|
|
59
|
+ windowHeight: $(window).height()
|
|
60
|
+ };
|
|
61
|
+ return this;
|
|
62
|
+ };
|
|
63
|
+
|
|
64
|
+ Plugin.prototype.positionSidebar = function () {
|
|
65
|
+ if (this.lastScrollTop > this.boundaries.contentTop &&
|
|
66
|
+ this.lastScrollTop < this.boundaries.contentBottom) {
|
|
67
|
+ this[this.scrollDirection === 'DOWN' ? 'scrollDown' : 'scrollUp']();
|
|
68
|
+ } else if (this.lastScrollTop < this.boundaries.contentTop) {
|
|
69
|
+ this.sidebar.css('top', '').removeClass('top-fixed');
|
|
70
|
+ }
|
|
71
|
+ return this;
|
|
72
|
+ };
|
|
73
|
+
|
|
74
|
+ Plugin.prototype.scrollDown = function () {
|
|
75
|
+ var windowScroll = this.lastScrollTop + this.boundaries.windowHeight,
|
|
76
|
+ sidebarOffsetTop;
|
|
77
|
+ if (this.sidebar.hasClass('scrolling-up')) {
|
|
78
|
+ this.sidebar.removeClass('scrolling-up')
|
|
79
|
+ .addClass('scrolling-down');
|
|
80
|
+ } else if (this.sidebar.hasClass('top-fixed')) {
|
|
81
|
+ sidebarOffsetTop = this.sidebar.offset().top - this.boundaries.contentTop;
|
|
82
|
+ this.sidebar.removeClass('top-fixed')
|
|
83
|
+ .css({
|
|
84
|
+ position: 'absolute',
|
|
85
|
+ top: sidebarOffsetTop
|
|
86
|
+ })
|
|
87
|
+ .addClass('scrolling-down');
|
|
88
|
+ }
|
|
89
|
+ if (this.sidebar.hasClass('scrolling-down')) {
|
|
90
|
+ if (windowScroll > this.sidebar.offset().top + this.boundaries.sidebarHeight) {
|
|
91
|
+ this.sidebar.css(clearPosition)
|
|
92
|
+ .addClass('bottom-fixed')
|
|
93
|
+ .removeClass('scrolling-down');
|
|
94
|
+ }
|
|
95
|
+ } else {
|
|
96
|
+ if (windowScroll > this.boundaries.contentBottom) {
|
|
97
|
+ this.sidebar.removeClass('bottom-fixed').css({
|
|
98
|
+ position: 'absolute',
|
|
99
|
+ top: this.boundaries.contentHeight - this.boundaries.sidebarHeight
|
|
100
|
+ });
|
|
101
|
+ } else if (windowScroll + this.options.tolerance >
|
|
102
|
+ this.boundaries.sidebarHeight + this.boundaries.contentTop) {
|
|
103
|
+ this.sidebar.css(clearPosition)
|
|
104
|
+ .removeClass('top-fixed')
|
|
105
|
+ .addClass('bottom-fixed');
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+ };
|
|
109
|
+
|
|
110
|
+ Plugin.prototype.scrollUp = function () {
|
|
111
|
+ if (this.sidebar.hasClass('scrolling-down')) {
|
|
112
|
+ this.sidebar.removeClass('scrolling-down')
|
|
113
|
+ .addClass('scrolling-up');
|
|
114
|
+ } else if (this.sidebar.hasClass('bottom-fixed')) {
|
|
115
|
+ this.sidebar.css({
|
|
116
|
+ position: 'absolute',
|
|
117
|
+ top: this.sidebar.offset().top - this.boundaries.contentTop
|
|
118
|
+ }).removeClass('bottom-fixed').addClass('scrolling-up');
|
|
119
|
+ }
|
|
120
|
+ if (this.sidebar.hasClass('scrolling-up')) {
|
|
121
|
+ if (this.lastScrollTop < this.sidebar.offset().top) {
|
|
122
|
+ this.sidebar.css(clearPosition)
|
|
123
|
+ .addClass('top-fixed')
|
|
124
|
+ .removeClass('scrolling-up');
|
|
125
|
+ }
|
|
126
|
+ } else {
|
|
127
|
+ if (this.lastScrollTop < this.boundaries.contentTop) {
|
|
128
|
+ this.sidebar.css('position', '').removeClass('top-fixed');
|
|
129
|
+ } else if (this.lastScrollTop - this.options.tolerance <
|
|
130
|
+ this.boundaries.contentBottom - this.boundaries.sidebarHeight) {
|
|
131
|
+ this.sidebar.css(clearPosition)
|
|
132
|
+ .removeClass('bottom-fixed')
|
|
133
|
+ .addClass('top-fixed');
|
|
134
|
+ }
|
|
135
|
+ }
|
|
136
|
+ };
|
|
137
|
+
|
|
138
|
+ Plugin.prototype.setScrollDirection = function () {
|
|
139
|
+ var scrollTop = $(window).scrollTop();
|
|
140
|
+ this.scrollDirection = (scrollTop > this.lastScrollTop ? 'DOWN' : 'UP');
|
|
141
|
+ this.lastScrollTop = scrollTop;
|
|
142
|
+ return this;
|
|
143
|
+ };
|
|
144
|
+
|
|
145
|
+ $.fn[pluginName] = function (options) {
|
|
146
|
+ return this.each(function () {
|
|
147
|
+ if (!$.data(this, 'plugin_' + pluginName)) {
|
|
148
|
+ $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
|
|
149
|
+ }
|
|
150
|
+ });
|
|
151
|
+ };
|
|
152
|
+
|
|
153
|
+}(jQuery, window));
|