webpack-bootstrap-ui-kit/src/js/_components/_ui.sidebar.js

80 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-01-29 10:09:39 +01:00
'use strict';
2020-07-30 12:57:28 +02:00
//import StickySidebar from 'sticky-sidebar/src/sticky-sidebar';
2020-01-29 10:09:39 +01:00
import Events from '../_events';
const SidebarUI = (($) => {
const D = document;
const W = window;
2020-07-30 12:57:28 +02:00
const $Body = $('body');
2020-01-29 10:09:39 +01:00
const NAME = 'SidebarUI';
const CLASSNAME = `js${NAME}`;
2020-07-30 12:57:28 +02:00
const CONTENTHOLDER = 'content-holder';
const INNERWRAPPER = `${CLASSNAME}__inner`;
2020-01-29 10:09:39 +01:00
class SidebarUI {
static init() {
const ui = this;
ui.dispose();
2020-01-29 17:08:34 +01:00
if (!$(`.${CLASSNAME}`).length) {
return;
}
2020-01-29 10:09:39 +01:00
console.log(`Initializing: ${NAME}`);
2020-07-30 12:57:28 +02:00
//const fontSize = parseInt($Body.css('font-size'));
const fontSize = 0;
const contentElement = $(`.${CONTENTHOLDER}`)[0];
console.log(contentElement);
2020-01-29 10:09:39 +01:00
2020-07-30 12:57:28 +02:00
//$(`.${CLASSNAME}`).wrapInner(`<div class="${INNERWRAPPER}"></div>`);
const $el = $(`.${CLASSNAME}`);
const $innerWrapper = $(`.${INNERWRAPPER}`);
/*const sticky = new StickySidebar(`.${CLASSNAME}`, {
topSpacing: fontSize,
bottomSpacing: fontSize,
containerSelector: CONTENTHOLDER,
innerWrapperSelector: INNERWRAPPER,
});*/
$el.addClass(`${CLASSNAME}-active`);
2020-01-29 10:09:39 +01:00
$Body.on(`${Events.SCROLL} ${Events.RESIZE}`, (e) => {
2020-07-30 12:57:28 +02:00
const contentOffset = parseInt(contentElement.offsetTop) + fontSize;
const contentOffsetHeight = parseInt(contentElement.offsetHeight) - fontSize;
const sidebarWidth = $el[0].offsetWidth;
const scrollPos = parseInt($Body.scrollTop());
// normal pos
if(contentOffset >= scrollPos){
$innerWrapper.attr('style', '');
}else if(scrollPos >= (contentOffset + contentOffsetHeight - $innerWrapper[0].offsetHeight)){
// bottom pos
$innerWrapper.attr('style', `position:absolute;bottom:${fontSize}px`);
2020-07-30 12:57:28 +02:00
}else {
// scrolled pos
2020-07-30 12:57:28 +02:00
$innerWrapper.attr('style', `position:fixed;top:${fontSize}px;width:${sidebarWidth}px`);
}
2020-01-29 10:09:39 +01:00
});
}
static dispose() {
console.log(`Destroying: ${NAME}`);
}
}
2020-05-13 21:22:15 +02:00
$(W).on(`${Events.LODEDANDREADY}`, () => {
2020-01-29 10:09:39 +01:00
SidebarUI.init();
});
W.SidebarUI = new SidebarUI();
return SidebarUI;
})($);
export default SidebarUI;