所以我試圖創建一種方法來檢測身體何時調整大小,然後我抓住新的高度並通過postmessage發送它。功能不正確的部分是當我縮小身體的時候(當我移除(隱藏)一些元素以使身體縮小時)。由於window.body.scrollHeight只保存最後一個值,因此不會被檢測到。 這裏是我的代碼,我將不勝感激任何建議:window.body.scrollHeight在降低高度時不會改變
sendMessage();
checkDocumentHeight(sendMessage);
function sendMessage(){
var heightWidth = document.body.scrollHeight+ " " +document.body.scrollWidth;
parent.postMessage(heightWidth, "*");
}
function checkDocumentHeight(callback){
var lastHeight = document.body.scrollHeight, newHeight, timer;
(function run(){
newHeight = document.body.scrollHeight;
if(lastHeight != newHeight)
callback();
lastHeight = newHeight;
timer = setTimeout(run, 1000);
})();
}
window.resize不這樣做的嗎? –
你能創建一個簡單的例子來向我們展示小提琴嗎? – Huangism
你有沒有想過這個?我處於相同的位置。 – user1422348