简体   繁体   English

使用Jquery更改css属性时的事件检测

[英]Event detect when css property changed using Jquery

Is there a way to detect if the "display" css property of an element is changed (to whether none or block or inline-block...)? 有没有办法检测元素的“display”css属性是否被更改(无论是否为block或block或inline-block ......)? if not, any plugin? 如果没有,任何插件? Thanks 谢谢

Note 注意

Mutation events have been deprecated since this post was written, and may not be supported by all browsers. 自撰写本文以来, 变异事件已被弃用,并且可能并非所有浏览器都支持。 Instead, use a mutation observer . 相反,使用变异观察者

Yes you can. 是的你可以。 DOM L2 Events module defines mutation events ; DOM L2 Events模块定义了突变事件 ; one of them - DOMAttrModified is the one you need. 其中之一 - DOMAttrModified是您需要的。 Granted, these are not widely implemented, but are supported in at least Gecko and Opera browsers. 当然,这些并没有得到广泛实施,但至少在Gecko和Opera浏览器中得到了支持。

Try something along these lines: 尝试这些方面的东西:

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

document.documentElement.style.display = 'block';

You can also try utilizing IE's "propertychange" event as a replacement to DOMAttrModified . 您还可以尝试使用IE的“propertychange”事件来替代DOMAttrModified It should allow to detect style changes reliably. 它应该允许可靠地检测style更改。

You can use attrchange jQuery plugin . 你可以使用attrchange jQuery插件 The main function of the plugin is to bind a listener function on attribute change of HTML elements. 插件的主要功能是在HTML元素的属性更改上绑定侦听器函数。

Code sample: 代码示例:

$("#myDiv").attrchange({
    trackValues: true, // set to true so that the event object is updated with old & new values
    callback: function(evnt) {
        if(evnt.attributeName == "display") { // which attribute you want to watch for changes
            if(evnt.newValue.search(/inline/i) == -1) {

                // your code to execute goes here...
            }
        }
    }
});

You can use jQuery's css function to test the CSS properties, eg. 您可以使用jQuery的css函数来测试CSS属性,例如。 if ($('node').css('display') == 'block') . if ($('node').css('display') == 'block')

Colin is right, that there is no explicit event that gets fired when a specific CSS property gets changed. Colin是对的,没有明确的事件在特定的CSS属性发生变化时被触发。 But you may be able to flip it around, and trigger an event that sets the display, and whatever else. 但是你可以翻转它,并触发一个设置显示器的事件,以及其他任何东西。

Also consider using adding CSS classes to get the behavior you want. 还可以考虑使用添加CSS类来获得所需的行为。 Often you can add a class to a containing element, and use CSS to affect all elements. 通常,您可以向包含元素添加类,并使用CSS来影响所有元素。 I often slap a class onto the body element to indicate that an AJAX response is pending. 我经常将一个类打到body元素上,以指示AJAX响应处于挂起状态。 Then I can use CSS selectors to get the display I want. 然后我可以使用CSS选择器来获得我想要的显示。

Not sure if this is what you're looking for. 不确定这是否是您正在寻找的。

For properties for which css transition will affect, can use transitionend event, example for z-index: 对于css转换将影响的属性,可以使用transitionend事件,例如z-index:

 $(".observed-element").on("webkitTransitionEnd transitionend", function(e) { console.log("end", e); alert("z-index changed"); }); $(".changeButton").on("click", function() { console.log("click"); document.querySelector(".observed-element").style.zIndex = (Math.random() * 1000) | 0; }); 
 .observed-element { transition: z-index 1ms; -webkit-transition: z-index 1ms; } div { width: 100px; height: 100px; border: 1px solid; position: absolute; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="changeButton">change z-index</button> <div class="observed-element"></div> 

You can't. 你不能。 CSS does not support "events". CSS不支持“事件”。 Dare I ask what you need it for? 敢问我需要什么? Check out this post here on SO. 在这里查看这篇文章 I can't think of a reason why you would want to hook up an event to a style change. 我想不出你为什么要把事件挂钩到样式改变的原因。 I'm assuming here that the style change is triggered somwhere else by a piece of javascript. 我在这里假设样式更改是通过一段javascript在其他地方触发的。 Why not add extra logic there? 为什么不添加额外的逻辑呢?

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

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