简体   繁体   English

JS监视CSS属性的变化,如“display:none”=>“display:block”?

[英]JS to monitor the CSS property change like “display:none”=>“display:block”?

I want to run some JS code when an image's CSS property "display" has been changed by any other JS script/functions. 当任何其他JS脚本/函数更改了图像的CSS属性“display”时,我想运行一些JS代码。 Is there any method to monitor that change and setup a callback function? 是否有任何方法来监视该更改并设置回调函数?

$(this).bind.('propertychange', function(){}) 

cannot do this, and setInterval is also a bad idea. 不能这样做,而setInterval也是一个坏主意。

What else could be done? 还有什么可以做的?

This is what you are looking for: 这就是你要找的东西:

document.documentElement.addEventListener('DOMAttrModified', function(e){
  if (e.attrName === 'style') {
    console.log('prevValue: ' + e.prevValue, 'newValue: ' + e.newValue);
  }
}, false);

Building upon Jeff's suggestion, I would recommend writing a single function that modifies the image style property and then using that function as the bottleneck that all other functions must go through to modify that image style property. 根据Jeff的建议,我建议编写一个修改图像样式属性的函数,然后使用该函数作为所有其他函数必须修改该图像样式属性的瓶颈。

function showImage(selector, callback) {
    $(selector).css("display","block");
    if(callback)
        callback();
}

function hideImage(selector, callback) {
    $(selector).css("display","none");
    if(callback)
        callback();
}

Something like the above two functions can be invoked from anywhere in your JavaScript when you must change the image CSS property. 当您必须更改图像CSS属性时,可以从JavaScript中的任何位置调用上述两个函数。 The functions also take a function as a parameter, which would be executed afterwards assuming the function was passed in as the 2nd parameter. 这些函数还将一个函数作为参数,该函数将在之后执行,假设函数作为第二个参数传入。

You could further simplify this into a single function, but I'll leave that to you as I don't know exactly what your goals are in doing this. 您可以进一步将其简化为单个功能,但我会将其留给您,因为我不确切知道您的目标是什么。

This is inside the legacy JavaScript files that you do not want to modify: 这是您不想修改的旧JavaScript文件中的内容:

// this is your original, unmodified function
function originalFunction(sel) {
    alert(sel);
    $(sel).css("display","none");
}

This is in your code: 这是在你的代码中:

// here is a sample callback function you pass into the extended function below
function myCallback(s) {
    alert("The image with src = '" + $(s).attr("src") + "' has been modified!");
}


// here is how you can extend the function to do what you want 
  // without needing to modify the actual code above
originalFunction = (function(legacyFn, callback) {

    // 1 arg function to be returned and reassigned to originalFunction
    return function(sel) {

        // call "original" originalFunction, with alert and image hide.
        legacyFn(sel);  

        if(callback) callback(sel);  // invoke your callback
    }

})(originalFunction, myCallback);

The variable originalFunction is assigned a function that takes one argument. 变量originalFunction被赋予一个带有一个参数的函数。 The function that takes one argument is returned by an anonymous, self-executing function that takes 2 arguments, the reference to the originalFunction before it is modified, and the reference to the callback function. 带有一个参数的函数由一个匿名的自执行函数返回,该函数接受2个参数,在修改之前对originalFunction的引用,以及对callback函数的引用。 These two function references become "locked" inside the closure so that when the originalFunction is then assigned a new value by the self-executing function, the legacyFn parameter still contains a reference to the originalFunction prior to it being modified. 这两个功能的引用成为“锁定”封闭的内部,使得当originalFunction然后分配由自执行功能的新值时, legacyFn参数仍然包含于一个参考originalFunction之前它被修改。

In summary, at a higher level, originalFunction and myCallback are passed in as parameters to the self-executing anonymous function and are passed into the variables legacyFn and callback, and a new function is then assigned to originalFunction . 总之,在更高级别, originalFunctionmyCallback作为参数传递给自执行匿名函数,并传递给变量legacyFn和callback,然后将新函数分配给originalFunction

Now, when you call originalFunction('.someClassOnAnImage') , the legacyFn will fire, which will alert the selector and set the display property to none. 现在,当您调用originalFunction('.someClassOnAnImage') ,将触发legacyFn,它将提醒选择器并将display属性设置为none。 Afterwards, the callback function, if it exists, will fire, and you'll then see: 之后,回调函数(如果存在)将触发,然后您将看到:

The image with src = '.someClassOnAnImage' has been modified!

While this isn't as nice as a hypothetical or platform-specific addEventListener, it does allow you to modify the behavior of the functions in the legacy code without having to physically crack open those files and modify them. 虽然这不如假设或特定于平台的addEventListener好,但它允许您修改遗留代码中函数的行为,而无需物理破解这些文件并对其进行修改。 This simply extends the functions to perform additional behaviors but without needing to modify the original functions or even the original files for that matter. 这只是扩展了函数以执行其他行为,但无需修改原始函数甚至原始文件。

You could neatly include all of your extensions in a separate JavaScript file (or whatever JavaScript file you're working in) and if you ever want to go back to the original behavior, you simply remove your extended functions. 您可以将所有扩展名整齐地包含在单独的JavaScript文件中(或者您正在使用的任何JavaScript文件中),如果您想要回到原始行为,只需删除扩展函数即可。

The Answer: See this other post >> is there an alternative to DOMAttrModified that will work in webkit 答案:看到这篇文章>> 有没有替代DOMAttrModified将在webkit中工作

The Rant: The DOM Mutation events hold the key to your problem. Rant:DOM Mutation事件是您解决问题的关键。 However, in the new wave of browser wars, Wekit and Gecko can't agree on stuff. 然而,在新一轮的浏览器大战中,Wekit和Gecko无法达成共识。 While Gecko has DOMAttrModified, webkit has something called mutation observer (which breaks the pattern of event handlers being attached to events but hey who cares for consistency when we want to lock users/coders in right? ;) 虽然Gecko有DOMAttrModified,但是webkit有一个叫做变异观察者的东西(它打破了附加到事件的事件处理程序的模式,但是当我们想要锁定用户/编码器时,谁会关心一致性?)

PS: Just adding this here for future seekers of the same wisdom. PS:在这里添加这个为未来寻求同样智慧的人。

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

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