function getWindowSize() {
// 获取窗口的宽度和高度,考虑滚动条和边框等因素
let width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
let height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
return { width, height };
}
// 示例用法:
let windowSize = getWindowSize();
console.log(`窗口宽度:${windowSize.width}px,窗口高度:${windowSize.height}px`);
// 更健壮的版本,处理兼容性问题和不同浏览器下的差异:
function getWindowSizeRobust() {
let width, height;
if (typeof window.innerWidth !== 'undefined') {
width = window.innerWidth;
height = window.innerHeight;
} else if (typeof document.documentElement !== 'undefined