Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More config params + only append style tag once #41

Merged
merged 3 commits into from
Apr 25, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions js/theia-sticky-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
'updateSidebarHeight': true,
'minWidth': 0,
'disableOnResponsiveLayouts': true,
'sidebarBehavior': 'modern'
'sidebarBehavior': 'modern',
'defaultPosition': 'relative',
'namespace': 'TSS'
};
options = $.extend(defaults, options);

Expand All @@ -34,7 +36,7 @@
if (!success) {
console.log('TSS: Body width smaller than options.minWidth. Init is delayed.');

$(document).scroll(function (options, $that) {
$(document).on('scroll.' + options.namespace, function (options, $that) {
return function (evt) {
var success = tryInit(options, $that);

Expand All @@ -43,7 +45,7 @@
}
};
}(options, $that));
$(window).resize(function (options, $that) {
$(window).on('resize.' + options.namespace, function (options, $that) {
return function (evt) {
var success = tryInit(options, $that);

Expand Down Expand Up @@ -75,7 +77,10 @@
options.initialized = true;

// Add CSS
$('head').append($('<style>.theiaStickySidebar:after {content: ""; display: table; clear: both;}</style>'));
var existingStylesheet = $('#theia-sticky-sidebar-stylesheet-' + options.namespace);
if (existingStylesheet.length === 0) {
$('head').append($('<style id="theia-sticky-sidebar-stylesheet-' + options.namespace + '">.theiaStickySidebar:after {content: ""; display: table; clear: both;}</style>'));
}

$that.each(function () {
var o = {};
Expand All @@ -94,7 +99,7 @@
// Create sticky sidebar
o.sidebar.parents().css('-webkit-transform', 'none'); // Fix for WebKit bug - https://code.google.com/p/chromium/issues/detail?id=20574
o.sidebar.css({
'position': 'relative',
'position': o.options.defaultPosition,
'overflow': 'visible',
// The "box-sizing" must be set to "content-box" because we set a fixed height to this element when the sticky sidebar has a fixed position.
'-webkit-box-sizing': 'border-box',
Expand Down Expand Up @@ -298,12 +303,12 @@
o.onScroll(o);

// Recalculate the sidebar's position on every scroll and resize.
$(document).scroll(function (o) {
$(document).on('scroll.' + o.options.namespace, function (o) {
return function () {
o.onScroll(o);
};
}(o));
$(window).resize(function (o) {
$(window).on('resize.' + o.options.namespace, function (o) {
return function () {
o.stickySidebar.css({'position': 'static'});
o.onScroll(o);
Expand Down Expand Up @@ -360,5 +365,7 @@

return width;
}

return this;
}
})(jQuery);