2013-12-19 17 views
1

所以我試圖創建一種方法來檢測身體何時調整大小,然後我抓住新的高度並通過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); 
     })(); 
    } 
+0

window.resize不這樣做的嗎? –

+0

你能創建一個簡單的例子來向我們展示小提琴嗎? – Huangism

+0

你有沒有想過這個?我處於相同的位置。 – user1422348

回答

0

scrollHeight屬性與不改變,你要使用的innerHeight。 scrollHeight與文檔高度基本相同。這是滾動條的高度。

window.innerHeight 

http://jsfiddle.net/gUUYc/4/

+0

這實際上返回undefined在我身邊。 –

+0

你看過小提琴嗎?它不應該返回undefined –