hexo-theme-keep/source/js/left-side-toggle.js

45 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-01-25 10:08:52 +08:00
/* global KEEP */
function initLeftSideToggle() {
2020-11-20 12:02:22 +08:00
KEEP.utils.leftSideToggle = {
2021-01-25 10:08:52 +08:00
toggleBar: document.querySelector('.page-aside-toggle'),
pageTopDom: document.querySelector('.page-main-content-top'),
containerDom: document.querySelector('.page-container'),
leftAsideDom: document.querySelector('.page-aside'),
toggleBarIcon: document.querySelector('.page-aside-toggle i'),
isOpenPageAside: false,
initToggleBarButton() {
2021-01-25 10:08:52 +08:00
this.toggleBar && this.toggleBar.addEventListener('click', () => {
this.isOpenPageAside = !this.isOpenPageAside;
KEEP.styleStatus.isOpenPageAside = this.isOpenPageAside;
KEEP.setStyleStatus();
this.changePageLayoutWhenOpenToggle(this.isOpenPageAside);
});
},
changePageLayoutWhenOpenToggle(isOpen) {
2021-01-25 10:08:52 +08:00
this.toggleBarIcon && (this.toggleBarIcon.className = isOpen ? 'fas fa-outdent' : 'fas fa-indent');
2020-11-25 23:39:24 +08:00
const pageAsideWidth = KEEP.theme_config.style.left_side_width || '260px';
this.containerDom.style.paddingLeft = isOpen ? pageAsideWidth : '0';
this.pageTopDom.style.paddingLeft = isOpen ? pageAsideWidth : '0';
this.leftAsideDom.style.left = isOpen ? '0' : `-${pageAsideWidth}`;
},
2021-01-25 10:08:52 +08:00
pageAsideHandleOfTOC(isOpen) {
this.toggleBar.style.display = 'flex';
this.isOpenPageAside = isOpen;
this.changePageLayoutWhenOpenToggle(isOpen);
}
}
2020-11-20 12:02:22 +08:00
KEEP.utils.leftSideToggle.initToggleBarButton();
}
2021-01-25 10:08:52 +08:00
if (KEEP.theme_config.pjax.enable === true && KEEP.utils) {
initLeftSideToggle();
} else {
window.addEventListener('DOMContentLoaded', initLeftSideToggle);
}