mirror of
https://github.com/StarCitizenTools/mediawiki-skins-Citizen.git
synced 2024-11-11 16:48:54 +00:00
refactor(core): ♻️ remove redundant intersection observers
This commit is contained in:
parent
8640d4ef30
commit
ca1f31d789
|
@ -29,27 +29,6 @@ function initDirectionObserver( onScrollDown, onScrollUp, threshold ) {
|
|||
window.addEventListener( 'scroll', throttledOnScroll );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an observer based on element visiblity.
|
||||
* Based on Vector
|
||||
*
|
||||
* @param {Function} onHidden functionality for when the element is visible
|
||||
* @param {Function} onVisible functionality for when the element is hidden
|
||||
* @return {IntersectionObserver}
|
||||
*/
|
||||
function initIntersectionObserver( onHidden, onVisible ) {
|
||||
/* eslint-disable-next-line compat/compat */
|
||||
return new IntersectionObserver( ( [ entry ] ) => {
|
||||
if ( entry.isIntersecting ) {
|
||||
// Viewport is within the target element.
|
||||
onVisible();
|
||||
} else if ( entry.boundingClientRect.top < 0 ) {
|
||||
// Viewport has crossed the bottom edge of the target element.
|
||||
onHidden();
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an observer for showing/hiding feature and for firing scroll event hooks.
|
||||
*
|
||||
|
@ -72,6 +51,5 @@ function initScrollObserver( show, hide ) {
|
|||
|
||||
module.exports = {
|
||||
initDirectionObserver,
|
||||
initIntersectionObserver,
|
||||
initScrollObserver
|
||||
};
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const SCROLL_DOWN_CLASS = 'citizen-scroll--down';
|
||||
const SCROLL_UP_CLASS = 'citizen-scroll--up';
|
||||
const STICKY_CLASS = 'citizen-page-header--sticky';
|
||||
const { initDirectionObserver, initIntersectionObserver } = require( './scrollObserver.js' );
|
||||
const { initDirectionObserver, initScrollObserver } = require( './scrollObserver.js' );
|
||||
|
||||
/**
|
||||
* Observes the scroll direction and adds/removes corresponding classes to the body element.
|
||||
|
@ -84,7 +84,7 @@ function init() {
|
|||
toggleStickyHeader( true );
|
||||
}, 250 );
|
||||
|
||||
const observer = initIntersectionObserver(
|
||||
const observer = initScrollObserver(
|
||||
() => {
|
||||
toggleStickyHeader( true );
|
||||
window.addEventListener( 'resize', onResize );
|
||||
|
|
Loading…
Reference in a new issue