-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to position fixed toc above footer is it overlaps
- Loading branch information
1 parent
c4b0f71
commit f340d58
Showing
4 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
source/wp-content/themes/wporg-developer/js/table-of-contents.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Table of content JS. | ||
* | ||
* Stops the sticky table of contents on wide screens from overlapping the footer | ||
* by positioning it slightly above | ||
*/ | ||
|
||
document.body.onload = function () { | ||
const toc = document.querySelector( '.table-of-contents' ); | ||
|
||
if ( ! toc ) { | ||
return; | ||
} | ||
|
||
const resetToCPosition = () => { | ||
if ( toc.style ) { | ||
toc.removeAttribute( 'style' ); | ||
} | ||
}; | ||
|
||
const tocBottom = toc.getBoundingClientRect().bottom; | ||
const footer = document.querySelector( '.global-footer' ); | ||
const windowHeight = window.innerHeight; | ||
|
||
const setToCPosition = () => { | ||
if ( window.getComputedStyle( toc ).position !== 'fixed' ) { | ||
resetToCPosition(); | ||
return; | ||
} | ||
|
||
const footerTop = footer.getBoundingClientRect().top; | ||
const exposedFooter = windowHeight - footerTop + 30; | ||
// if the bottom of the toc is less than the exposed footer move it to that position | ||
if ( windowHeight - tocBottom < exposedFooter ) { | ||
toc.style.bottom = exposedFooter + 'px'; | ||
toc.style.top = 'initial'; | ||
} else { | ||
resetToCPosition(); | ||
} | ||
}; | ||
|
||
setToCPosition(); | ||
|
||
window.addEventListener( 'scroll', setToCPosition ); | ||
window.addEventListener( 'resize', setToCPosition ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.