我想检查一个数字是否从另一个数字增加或减少到一定数量。例如:Let a = 20;Let b = 20; 如果b从什么增加了 5,a则返回true。如果b从什么减少了 5,a则返回true。提前感谢您的任何帮助。var lastHeightSize = document.body.scrollHeight;function checkBodySizeChange () { // instead of checking if lastHeightSize is not equal to document.body.scrollheight, // check if document.body.scrollheight has increased or decreased by 20 const heightChanged = lastHeightSize !== document.body.scrollHeight; if (heightChanged) { trigger(document.body, 'sizechange'); lastHeightSize = document.body.scrollHeight; } window.requestAnimationFrame(checkBodySizeChange);}
2 回答
HUWWW
TA贡献1874条经验 获得超12个赞
这是反映 rlemons 答案的更新代码。
var lastHeightSize = document.body.scrollHeight;
function checkBodySizeChange () {
const heightChanged = Math.abs(lastHeightSize - document.body.scrollHeight) > 120;
if (heightChanged) {
trigger(document.body, 'sizechange');
lastHeightSize = document.body.scrollHeight;
}
window.requestAnimationFrame(checkBodySizeChange);
}
添加回答
举报
0/150
提交
取消