JavaScript/jQuery:移除"display: none"时如何触发事件?

JavaScript/jQuery: How to trigger an event when "display: none" is removed?

如果之前有人回答过这个问题,我很抱歉,我无法使用搜索找到它。

我必须标记 4 个元素才能在它们未隐藏时触发事件("display: none" 已删除)。我认为 mutationObserver 是正确的方法,但我无法过滤到我需要的事件。

有人有这方面的经验吗?

编辑:不幸的是,我们无法访问实际取消隐藏元素的脚本。它们由另一个团队拥有和保护。我们唯一可以关注的是元素何时取消隐藏。

我在想办法根据我的需要编辑它。

    // Blocker is the element that has a changing display value
var blocker  = document.querySelector( '#blocker' );
// Trigger changes the display value of blocker
var trigger  = document.querySelector( '#trigger' );
// Our mutation observer, which we attach to blocker later
var observer = new MutationObserver( function( mutations ){
    mutations.forEach( function( mutation ){
        // Was it the style attribute that changed? (Maybe a classname or other attribute change could do this too? You might want to remove the attribute condition) Is display set to 'none'?
        if( mutation.attributeName === 'style' && window.getComputedStyle( blocker ).getPropertyValue( 'display' ) !== 'none'
          ){
            alert( '#blocker\'s style just changed, and its display value is no longer \'none\'' );
        }
    } );
} );

// Attach the mutation observer to blocker, and only when attribute values change
observer.observe( blocker, { attributes: true } );

// Make trigger change the style attribute of blocker
trigger.addEventListener( 'click', function(){
    blocker.removeAttribute( 'style' );
}, false );

不幸的是,我不确定你是如何 "removing" display: none,但是,如果你碰巧使用 jQuery 的 $.show 函数,那么您可以指定一个 "callback" 函数,该函数将在节目 "animation" 结束后调用。

这是一个例子:

$(".myElement").show(400, function() {
    // Do something...
});

同样,我不确定 如何删除 display: none,所以这可能根本没有帮助。

开始了

var blocker  = document.querySelector('#blocker');
var previousValue = blocker.style.display;

var observer = new MutationObserver( function(mutations){
    mutations.forEach( function(mutation) {
        if (mutation.attributeName !== 'style') return;
        var currentValue = mutation.target.style.display;
        if (currentValue !== previousValue) {
           if (previousValue === "none" && currentValue !== "none") {
             console.log("display none has just been removed!");
           }

           previousValue = mutation.target.style.display;
        }
    });
});


observer.observe(blocker, { attributes: true });

基本上我们得到了样式属性中设置的默认值(如果有的话)——这存储在previousValue中;然后我们留下代码,但我们在循环中做了一些不同。首先,我们检查目标中的样式是否已更改。然后我们在目标的样式属性中获取当前显示值。下一步是比较这些值。如果 previousValue 是 "none" 而现在是其他值,这意味着显示:none 未设置。但是,您必须注意,它只侦听此特定更改。如果你不需要监听 "none" 那么你可以忽略它。

您可以在您的代码中使用它 - 我并不是故意编写您的整个代码,所以请随意在目标上添加您的事件侦听器。

我发现还有另一种方法可以在元素被 display:none 隐藏或变得可见时可靠地触发事件。

这基于 ResizeObserver API,它应该适用于所有现代浏览器 (https://developer.mozilla.org/en-US/docs/Web/API/Resize_Observer_API)

当元素应用 css 规则 display:none 时,其所有维度都将为零。因此,为了检测变得可见,我们只需要观察一个在可见时具有非零维度的元素。

const block=document.querySelector("#the-block")
const resizewatcher=new ResizeObserver(entries => {
  for (const entry of entries){
    console.log("Element",entry.target, 
      (entry.contentRect.width == 0) ? 
      "is now hidden" : 
      "is now visible"
    )
  }
})
resizewatcher.observe(block)