简体   繁体   English

有没有一种方法可以使IE 10+在元素的溢出发生变化时触发事件?

[英]Is there a way to get IE 10+ to fire an event when the overflow of an element changes?

I want to test if a div is overflowing. 我想测试div是否溢出。 The following code works for webkit and FF. 以下代码适用于webkit和FF。

    var div = $("#myDivWithOverflow")[0];
    var OnOverflowChanged = function(){
        alert("OnOverflowChanged");
    }

    if (div.addEventListener) {
        // webkit
        div.addEventListener ('overflowchanged', OnOverflowChanged, false);
        // firefox
        div.addEventListener ('overflow', OnOverflowChanged, false);
    }

As far as I can see IE10+ has support for the addEventListener method, but does not react on either of the above events. 据我所知,IE10 +支持addEventListener方法,但对以上任何一个事件都没有反应。

Is there a way to achieve this in IE? 有没有办法在IE中实现这一目标?

Update : I've created a fiddle which illustrates clearer what I want to achieve: http://jsfiddle.net/AyKarsi/sB36r/1/ Unfortunately, the fiddle does not work at all in IE due to some security restrictions 更新 :我创建了一个小提琴,该小提琴更清楚地说明了我想要实现的目标: http : //jsfiddle.net/AyKarsi/sB36r/1/不幸的是,由于某些安全限制,该小提琴根本无法在IE中使用

Currently, overflowchanged are only supported on Webkit browsers. 当前,仅Webkit浏览器支持overflowchanged

A work around can be to check for mutation events (notably DOMAttrModified ) and check if the overflow changed. 解决方法是检查突变事件(尤其是DOMAttrModified ),并检查溢出是否已更改。

div.addEventListener("DOMAttrModified", function() {
  // check overflow value
}, false);

This won't trigger on screen resize, so it may not do exactly what you're looking for. 这不会在屏幕调整大小时触发,因此它可能无法完全满足您的需求。 But it's somewhere you can dig for a solution. 但这是您可以寻求解决方案的地方。

On MDN: https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Events/Mutation_events 在MDN上: https//developer.mozilla.org/zh-CN/docs/Web/Guide/API/DOM/Events/Mutation_events
On Microsoft site: http://msdn.microsoft.com/en-us/library/ie/dn265032(v=vs.85).aspx 在Microsoft网站上: http : //msdn.microsoft.com/en-us/library/ie/dn265032(v=vs.85).aspx

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

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