简体   繁体   English

当另一个可见时隐藏一个div

[英]Hide a div when another is visible

I am trying to hide a search box with the class .search-container-inner input when the menu overlay is shown.当显示菜单覆盖时,我试图隐藏一个带有类.search-container-inner input的搜索框。 It has a class of .overlay.style-light-bg .它有一个.overlay.style-light-bg

I can't get this to work with CSS, as they have no direct relationship and the search box is before the overlay in the HTML.我无法让它与 CSS 一起使用,因为它们没有直接关系,并且搜索框位于 HTML 中的叠加层之前。

I tried editing this solution from a previous post:我尝试从以前的帖子中编辑此解决方案:

// Show an element
var show = function (elem) {
    elem.style.display = 'block';
};

// Hide an element
var hide = function (elem) {
    elem.style.display = 'none';
};

// Toggle element visibility
var toggle = function (elem) {

    // If the element is visible, hide it
    if (window.getComputedStyle(elem).display === 'block') {
        hide(elem);
        return;
    }

    // Otherwise, show it
    show(elem);

    };

But to no avail.但无济于事。

This an aproximation of will be:这将是:

var toggle = function (elem) {
if(!elem.classList.contains(class);)
elem.classList.toggle('hidden');
}

I don't know how the overlay is called or presented, but you could check if the class exists and then change display to the search box like this:我不知道如何调用或呈现叠加层,但您可以检查该类是否存在,然后将display更改为搜索框,如下所示:

var searchInput = document.querySelector(".search-container-inner input");

if (document.querySelector(".overlay.style-light-bg")) { // check if exists
  searchInput.style.display = "none";
} else {
  searchInput.style.display = "block";
}

It would run once, but you also could wrap it into a function to be called whenever you want.它会运行一次,但您也可以将其包装成一个函数,以便随时调用。

Since you have already laid the groundwork with your code writing the show hide and toggle functions, all that is left is call them, based on your requirement.由于您已经为编写show hidetoggle功能的代码奠定了基础,剩下的就是根据您的要求调用它们。

var overlay = document.querySelector('.overlay.style-light-bg');
var search = document.querySelector('.search-container-inner input');

if (window.getComputedStyle(overlay).display === 'block') {
    hide(search);
    return;
}

Now, call the above code when you are showing the overlay现在,在显示叠加层时调用上面的代码

You could use MutationObserver to achieve the desired behaviour.您可以使用MutationObserver来实现所需的行为。 It provides the ability to watch for specific changes made to the DOM tree and invoke a callback function to handle the change.它提供了监视对 DOM 树所做的特定更改并调用回调函数来处理更改的能力。 It has browser support back to IE 11.它具有回到 IE 11 的浏览器支持。

Here is the skeleton code including the magic doSomething() function.这是包含魔术doSomething()函数的骨架代码。

function doSomething() {
  var overlay = window.getComputedStyle(document.querySelector('.overlay.style-light-bg')).display
  var searchInput = document.querySelector('.search-container-inner input')
  searchInput.style.display = (overlay == 'block') ? 'none' : 'block'      
}

// The following code uses MutationObserver to call `doSomething()` when needed

// Handle to the DOM element that will be observed
var overlayNode = document.querySelector('.overlay.style-light-bg')
// Configure the observer to watch for changes to the element's attributes only
var observerConfig = { attributes: true }

// Callback function to execute when mutations are observed
var observerCallback = function(mutationsList, observer) {
  for (var mutation of mutationsList) {
    if (mutation.type == 'attributes') {
      console.log('The ' + mutation.attributeName + ' attribute was modified.')
      if (mutation.attributeName == 'style' || mutation.attributeName == 'class') {
        // handle the mutation of the overlay element's style or class list
        doSomething()
      }
    }
  }
}

// Create a `MutationObserver` instance linked to the `observerCallback` function
var overlayObserver = new MutationObserver(observerCallback);

// Start observing the overlay node for configured mutations
overlayObserver.observe(overlayNode, observerConfig);

Depending on how the overlay was mutated, the console output will display:根据叠加的变化方式,控制台输出将显示:

The style attribute was modified.

or或者

The class attribute was modified.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM