所以我正在尝试创建一种方法来检测 body 何时调整大小,然后我获取新的高度并通过邮件发送它。功能不正常的部分是当我缩小 body 尺寸时(当我删除(隐藏)一些元素使 body 缩小时)。这不会被检测到,因为 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);
})();
}
最佳答案
scrollHeight用不着变,要用innerHeight。 scrollHeight 与文档高度基本相同。它是滚动条的高度。
window.innerHeight
关于javascript - window.body.scrollHeight 在降低高度时不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20692216/