這是我爲我正在構建的應用程序開發的解決方法。它在基礎選項卡控件中具有多個元素。
我用MutationObserver
觀察時的父元素(一個基金會div.tabs-content div.content
元素)變得active
,然後我切換iframe
的文檔的overflow
財產。運行時效果是不可思議的。
我本來想observe
的直接,但是因爲從技術上來講element.style
值沒有DOM突變事件時的iframe本身已經改變display
財產提出,我想是不是DOM結構適當的一部分。
這是我的代碼(Vanilla.js,沒有jQuery)。如果你正在使用你的應用程序,你會想要的東西,是適用於您的文檔來代替我的可視性,檢測代碼:
window.addEventListener('DOMContentLoaded', function(e) {
var observer = new MutationObserver(onContentMutated);
var options = { attributes: true, childList: false, characterData: false, subtree: false, attributeFilter: ['class'] };
var iframeContainers = document.querySelectorAll('.tabs-content .content');
for(var i = 0; i < iframeContainers.length; i++) {
observer.observe(iframeContainers[i], options);
}
});
function onContentMutated(mutations) {
for(var i = 0; i < mutations.length; i++) {
var m = mutations[i];
var thisIsNowAnActiveTab = m.target.classList.contains('active');
if(thisIsNowAnActiveTab) {
// get the corresponding iframe and fiddle with its DOM
var iframes = m.target.getElementsByTagName("iframe");
if(iframes.length == 0) continue;
var iframe = iframes[0];
iframe.contentWindow.document.documentElement.style.overflow = 'hidden';
// the timeout is to trigger Chrome to recompute the necessity of the scrollbars, which makes them visible again. Because the timeout period is 0 there should be no visible change to users.
setTimeout(function(s) {
s.overflow = 'auto';
}, 0, iframe.contentWindow.document.documentElement.style);
}
console.log(m.type);
}
}
來源
2016-11-27 08:34:20
Dai
看來,錯誤出現在Chrome 52.0.2743.82:http://googlechromereleases.blogspot.fr/2016/07/stable-channel-update.html – lepix