0

I have a mega menu element with class .dropdownOpen activated when it is hovered. Meanwhile I have to change CSS on a completely separate div outside the header div

I have to add this CSS when .dropdownOpen is acivated by JS to

#theme-page {
    filter: blur(8px);
    -webkit-filter: blur(8px);
}

What could be the best solution? I tried pure CSS but I was not able to do it.

EDIT:

Here -was- a live example website.

When hovering on Mega menu element, there is a class added .dropdownOpen just after .has-mega-menu here:

<li id="menu-item-1068" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children has-mega-menu"><a class="menu-item-link js-smooth-scroll" href="/4" aria-haspopup="true">Mega menu</a>

If that class is added I need to blur the whole div #theme-page which is located way more down below.

twelvell
  • 257
  • 1
  • 8
  • 19

2 Answers2

1

You added a tag with jQuery so we can use it and do:

jQuery('.has-mega-menu').hover(() => jQuery('#theme-page').addClass('blur'), () => jQuery('#theme-page').removeClass('blur'))

and add some css for our blur class:

.blur {
    filter: blur(8px);
    -webkit-filter: blur(8px);
}
Buszmen
  • 1,978
  • 18
  • 25
  • Thanks @buszmen working! btw how can I add a small delay to it? – twelvell Feb 09 '20 at 00:49
  • You can use css `animation` and `keyframes` to create 3 steps: 0%, 99% with no blur and 100% with full blur. The animation duration will be your delay. If you want to smooth out the blurring, just change 99% to something smaller and play with the duration. – Buszmen Feb 09 '20 at 00:54
0

You could use the MutationObserver to detect the class change.

You can read more about MutationObserver here

function addclass() {
  document.getElementById("dropdown").classList.toggle("dropdownOpen");
}

(function() {
  var e = document.getElementById("dropdown"); // the maga menu element
  var observer = new MutationObserver(function(event) {
    let otherDiv = document.getElementById("otherDiv");
    if (/.*dropdownOpen.*/.test(e.className)) {
      otherDiv.classList.add("theme-page");
    } else {
      otherDiv.classList.remove("theme-page");
    }
  });
  observer.observe(e, {
    attributes: true,
    attributeFilter: ["class"],
    childList: false,
    characterData: false
  });
})();
.dropdownOpen {
  background-color: red;
}
.theme-page {
  background-color: green;
}
<div id="dropdown">Dropdown</div>
<div id="otherDiv">Other div</div>
<button onclick="addclass()">Add Class</button>
Sohail Ashraf
  • 10,078
  • 2
  • 26
  • 42