-
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 830fb7b
Showing
5 changed files
with
63 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
2 changes: 2 additions & 0 deletions
2
source/wp-content/themes/wporg-developer/js/rafThrottle.min.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
/** | ||
* Table of contents JS. | ||
* | ||
* Stops the sticky table of contents on wide screens from overlapping the footer | ||
* by positioning it slightly above | ||
* Uses global rafThrottle.min.js - https://github.com/wuct/raf-throttle | ||
*/ | ||
|
||
document.body.onload = () => { | ||
const toc = document.querySelector( '.table-of-contents' ); | ||
|
||
if ( ! toc ) { | ||
return; | ||
} | ||
|
||
const footer = document.querySelector( '.global-footer' ); | ||
const tocTop = parseInt( window.getComputedStyle( toc ).top, 10 ); | ||
let windowHeight = window.innerHeight; | ||
|
||
const resetToCPosition = () => { | ||
if ( toc.style ) { | ||
toc.removeAttribute( 'style' ); | ||
} | ||
}; | ||
|
||
const setToCPosition = () => { | ||
if ( window.getComputedStyle( toc ).position !== 'fixed' ) { | ||
resetToCPosition(); | ||
return; | ||
} | ||
|
||
const tocHeight = toc.offsetHeight; | ||
const tocBottom = tocTop + tocHeight; | ||
const footerTopWithClearSpace = parseInt( footer.getBoundingClientRect().top, 10 ) - 30; | ||
|
||
// if the bottom of the toc is below the top of the footer's clear space move it above | ||
if ( tocBottom > footerTopWithClearSpace ) { | ||
toc.style.bottom = windowHeight - footerTopWithClearSpace + 'px'; | ||
toc.style.top = 'initial'; | ||
} else { | ||
resetToCPosition(); | ||
} | ||
}; | ||
|
||
setToCPosition(); | ||
|
||
// eslint-disable-next-line no-undef | ||
window.addEventListener( 'scroll', rafThrottle( setToCPosition ) ); | ||
window.addEventListener( | ||
'resize', | ||
// eslint-disable-next-line no-undef | ||
rafThrottle( () => { | ||
windowHeight = window.innerHeight; | ||
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.