TOC optimizations
This commit is contained in:
@@ -14,6 +14,8 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
var headingOffset = 88;
|
var headingOffset = 88;
|
||||||
var ticking = false;
|
var ticking = false;
|
||||||
|
|
||||||
|
var tocBottomOffset = null;
|
||||||
|
|
||||||
function isDesktop() {
|
function isDesktop() {
|
||||||
return desktopQuery.matches;
|
return desktopQuery.matches;
|
||||||
}
|
}
|
||||||
@@ -34,6 +36,26 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getArticleElement() {
|
||||||
|
return document.querySelector(
|
||||||
|
'.single-post .post-content article, ' +
|
||||||
|
'.single-post .post-content, ' +
|
||||||
|
'article.post, article, ' +
|
||||||
|
'.entry-content'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTocBottomOffset() {
|
||||||
|
if (tocBottomOffset !== null) return tocBottomOffset;
|
||||||
|
|
||||||
|
var value = getComputedStyle(document.documentElement)
|
||||||
|
.getPropertyValue('--toc-bottom-offset')
|
||||||
|
.trim();
|
||||||
|
|
||||||
|
tocBottomOffset = parseInt(value, 10) || 12;
|
||||||
|
return tocBottomOffset;
|
||||||
|
}
|
||||||
|
|
||||||
function syncPosition() {
|
function syncPosition() {
|
||||||
if (!isDesktop()) {
|
if (!isDesktop()) {
|
||||||
document.documentElement.style.setProperty('--zeitfresser-toc-top', stickyTop + 'px');
|
document.documentElement.style.setProperty('--zeitfresser-toc-top', stickyTop + 'px');
|
||||||
@@ -84,25 +106,29 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var footer = document.querySelector('footer.site-footer, footer, #colophon');
|
var article = getArticleElement();
|
||||||
if (!footer) return;
|
|
||||||
|
if (!article) {
|
||||||
|
toc.style.transform = '';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
toc.style.transform = '';
|
toc.style.transform = '';
|
||||||
|
|
||||||
var scrollTop = window.scrollY || window.pageYOffset;
|
var scrollTop = window.scrollY || window.pageYOffset;
|
||||||
|
|
||||||
var footerTop = footer.getBoundingClientRect().top + scrollTop;
|
var articleRect = article.getBoundingClientRect();
|
||||||
|
var articleBottom = articleRect.top + scrollTop + articleRect.height;
|
||||||
|
|
||||||
var tocRect = toc.getBoundingClientRect();
|
var tocRect = toc.getBoundingClientRect();
|
||||||
var tocTop = tocRect.top + scrollTop;
|
var tocTop = tocRect.top + scrollTop;
|
||||||
var tocHeight = tocRect.height;
|
var tocHeight = tocRect.height;
|
||||||
var tocBottom = tocTop + tocHeight;
|
var tocBottom = tocTop + tocHeight;
|
||||||
|
|
||||||
var offset = 130;
|
var offset = getTocBottomOffset();
|
||||||
|
|
||||||
var maxBottom = footerTop - offset;
|
var maxBottom = articleBottom - offset;
|
||||||
|
var overflow = Math.ceil(tocBottom - maxBottom);
|
||||||
var overflow = tocBottom - maxBottom;
|
|
||||||
|
|
||||||
if (overflow > 0) {
|
if (overflow > 0) {
|
||||||
toc.style.transform = 'translateY(-' + overflow + 'px)';
|
toc.style.transform = 'translateY(-' + overflow + 'px)';
|
||||||
@@ -125,10 +151,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
function updateProgress() {
|
function updateProgress() {
|
||||||
if (!progressBar) return;
|
if (!progressBar) return;
|
||||||
|
|
||||||
var article = document.querySelector(
|
var article = getArticleElement();
|
||||||
'.single-post .post-content article, ' +
|
|
||||||
'.single-post .post-content, article.post, article'
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!article) {
|
if (!article) {
|
||||||
progressBar.style.width = '0%';
|
progressBar.style.width = '0%';
|
||||||
@@ -144,7 +167,6 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
|
|
||||||
function updateActiveHeading() {
|
function updateActiveHeading() {
|
||||||
var headings = getHeadings();
|
var headings = getHeadings();
|
||||||
|
|
||||||
if (!headings.length) return;
|
if (!headings.length) return;
|
||||||
|
|
||||||
var currentId = headings[0].target.id;
|
var currentId = headings[0].target.id;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Author: Zeitfresser
|
|||||||
Author URI: https://ztfr.eu/
|
Author URI: https://ztfr.eu/
|
||||||
Theme URI: https://ztfr.eu/
|
Theme URI: https://ztfr.eu/
|
||||||
Description: Zeitfresser Wordpress Theme
|
Description: Zeitfresser Wordpress Theme
|
||||||
Version: 1.2
|
Version: 1.3
|
||||||
Tested up to: 6.2
|
Tested up to: 6.2
|
||||||
Requires PHP: 7.0
|
Requires PHP: 7.0
|
||||||
License: GNU General Public License v2 or later
|
License: GNU General Public License v2 or later
|
||||||
@@ -34,6 +34,7 @@ html{line-height:1.15;-webkit-text-size-adjust:100%}body{margin:0}main{display:b
|
|||||||
:root{
|
:root{
|
||||||
--zeitfresser-toc-top-offset: 10px;
|
--zeitfresser-toc-top-offset: 10px;
|
||||||
--zeitfresser-toc-width: 230px;
|
--zeitfresser-toc-width: 230px;
|
||||||
|
--toc-bottom-offset: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.zeitfresser-floating-toc{
|
.zeitfresser-floating-toc{
|
||||||
|
|||||||
Reference in New Issue
Block a user