GreenShift Tips and Tutorials

Helper Icon Hider Script

Ivan
Ivan
 · 
This JS snippet complements the work done with Interaction Layers to show/hide various helper icons (cookie notices, chat widgets, back to top buttons, etc.) so that they do not cover the footer area.
// Configuration
const triggerClassName = 'footer-in-view';
const triggerElementSelector = 'footer';
const targetIds = [];
const targetClasses = ['mhcookie-preferences-toggle'];

const targetSelector = [
  ...targetIds.map(id => '#' + id),
  ...targetClasses.map(c => '.' + c)
].join(',');

function setTargetsVisible(visible) {
  const targets = document.querySelectorAll(targetSelector);
  console.log('Found elements:', targets.length);
  targets.forEach(el => {
    el.classList.toggle('gs-show', visible);
    el.classList.toggle('gs-hide', !visible);
  });
}

function init() {
  console.log('Initializing...');

  const trigger = document.querySelector(triggerElementSelector);
  if (!trigger) {
    console.warn('Trigger element "' + triggerElementSelector + '" not found.');
    return;
  }

  // Targets start visible and only hide on a real footer-in-view transition
  let inView = false;
  setTargetsVisible(true);

  new MutationObserver(() => {
    const now = trigger.classList.contains(triggerClassName);
    if (now === inView) return;
    inView = now;
    console.log(inView ? 'Footer in view - hiding targets' : 'Footer out of view - showing targets');
    setTargetsVisible(!inView);
  }).observe(trigger, { attributes: true, attributeFilter: ['class'] });
}

if (document.readyState === 'loading') {
  document.addEventListener('DOMContentLoaded', init);
} else {
  init();
}
JavaScript
This code snippet is used in the following tutorial(s):
Back to Code Bits Index

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only