js/jquery获取浏览器窗口可视区域高度和宽度以及滚动条高度实现代码 - Web前端
作者:98wpeu发布时间:2026-10-05分类:网页前端技术浏览:93
导读:获取浏览器窗口的可视区域高度和宽度,滚动条高度有需要的朋友可参考一下。ie中,浏览器显示窗口大小只能以下获取: 代码如下复制代码复制代码代码如下:document.body.o...
获取浏览器窗口的可视区域高度和宽度,滚动条高度有需要的朋友可参考一下。
ie中,浏览器显示窗口大小只能以下获取: 代码如下复制代码
复制代码 代码如下:
document.body.offsetwidth
document.body.offsetheight
在声明了DOCtype的浏览器中,可以用以下来获取浏览器显示窗口大小: 代码如下复制代码
复制代码 代码如下:
document.documentElement.clientWidth
document.documentElement.clIEntHeight
IE,FF,Safari皆支持该方法,opera虽支持该属性,但是返回的是页面尺寸;
同时,除了IE以外的所有浏览器都将此信息保存在Window对象中,可以用以下获取: 代码如下复制代码
复制代码 代码如下:
window.innerWidth
window.innerHeight
整个网页尺寸一般获得方法 代码如下复制代码
复制代码 代码如下:
document.body.scrollWidth
document.body.scrollHeight
屏幕分辨率高度一般获得方法 代码如下复制代码
复制代码 代码如下:
window.screen.height
window.screen.width
总结一下实例
复制代码 代码如下:
functiongetViewSizeWithoutScrollbar(){//不包含滚动条
return {
width : document.documentElement.clientWidth,
height: document.documentElement.clientHeight
}
}
function getViewSizeWithScrollbar(){//包含滚动条
if(window.innerWidth){
return {
width : window.innerWidth,
height: window.innerHeight
}
}else if(document.documentElement.offsetWidth == document.documentElement.clientWidth){
return {
width : document.documentElement.offsetWidth,
height: document.documentElement.offsetHeight
}
}else{
return {
width : document.documentElement.clientWidth + getScrollWith(),
height: document.documentElement.clientHeight + getScrollWith()
}
}
}
IE,firefox 差异如下:
IE6.0、FF1.06+:
复制代码 代码如下:
clientWidth = width + padding
clientHeight = height + padding
offsetWidth = width + padding + border
offsetHeight = height + padding + border
IE5.0/5.5:
clientWidth = width - border
clientHeight = height - border
offsetWidth = width
offsetHeight = height
另附个人最常用的获取整页宽高的方法(需要JQuery框架) 代码如下复制代码
复制代码 代码如下:
$(document).width() < $('body').width() ? $(document).width() : $('body').width();
$(document).height() < $('body').height() ? $(document).height() : $('body').height();
alert($(window).height()); //浏览器时下窗口可视区域高度
alert($(document).height()); //浏览器时下窗口文档的高度
alert($(document.body).height());//浏览器时下窗口文档body的高度
alert($(document.body).outerHeight(true));//浏览器时下窗口文档body的总高度 包括border padding margin
alert($(window).width()); //浏览器时下窗口可视区域宽度
alert($(document).width());//浏览器时下窗口文档对于象宽度
alert($(document.body).width());//浏览器时下窗口文档body的高度
alert($(document.body).outerWidth(true));//浏览器时下窗口文档body的总宽度 包括border padding margin
alert($(document).scrollTop()); //获取滚动条到顶部的垂直高度
alert($(document).scrollLeft()); //获取滚动条到左边的垂直宽度
ie中,浏览器显示窗口大小只能以下获取: 代码如下复制代码
复制代码 代码如下:
document.body.offsetwidth
document.body.offsetheight
在声明了DOCtype的浏览器中,可以用以下来获取浏览器显示窗口大小: 代码如下复制代码
复制代码 代码如下:
document.documentElement.clientWidth
document.documentElement.clIEntHeight
IE,FF,Safari皆支持该方法,opera虽支持该属性,但是返回的是页面尺寸;
同时,除了IE以外的所有浏览器都将此信息保存在Window对象中,可以用以下获取: 代码如下复制代码
复制代码 代码如下:
window.innerWidth
window.innerHeight
整个网页尺寸一般获得方法 代码如下复制代码
复制代码 代码如下:
document.body.scrollWidth
document.body.scrollHeight
屏幕分辨率高度一般获得方法 代码如下复制代码
复制代码 代码如下:
window.screen.height
window.screen.width
总结一下实例
复制代码 代码如下:
functiongetViewSizeWithoutScrollbar(){//不包含滚动条
return {
width : document.documentElement.clientWidth,
height: document.documentElement.clientHeight
}
}
function getViewSizeWithScrollbar(){//包含滚动条
if(window.innerWidth){
return {
width : window.innerWidth,
height: window.innerHeight
}
}else if(document.documentElement.offsetWidth == document.documentElement.clientWidth){
return {
width : document.documentElement.offsetWidth,
height: document.documentElement.offsetHeight
}
}else{
return {
width : document.documentElement.clientWidth + getScrollWith(),
height: document.documentElement.clientHeight + getScrollWith()
}
}
}
IE,firefox 差异如下:
IE6.0、FF1.06+:
复制代码 代码如下:
clientWidth = width + padding
clientHeight = height + padding
offsetWidth = width + padding + border
offsetHeight = height + padding + border
IE5.0/5.5:
clientWidth = width - border
clientHeight = height - border
offsetWidth = width
offsetHeight = height
另附个人最常用的获取整页宽高的方法(需要JQuery框架) 代码如下复制代码
复制代码 代码如下:
$(document).width() < $('body').width() ? $(document).width() : $('body').width();
$(document).height() < $('body').height() ? $(document).height() : $('body').height();
alert($(window).height()); //浏览器时下窗口可视区域高度
alert($(document).height()); //浏览器时下窗口文档的高度
alert($(document.body).height());//浏览器时下窗口文档body的高度
alert($(document.body).outerHeight(true));//浏览器时下窗口文档body的总高度 包括border padding margin
alert($(window).width()); //浏览器时下窗口可视区域宽度
alert($(document).width());//浏览器时下窗口文档对于象宽度
alert($(document.body).width());//浏览器时下窗口文档body的高度
alert($(document.body).outerWidth(true));//浏览器时下窗口文档body的总宽度 包括border padding margin
alert($(document).scrollTop()); //获取滚动条到顶部的垂直高度
alert($(document).scrollLeft()); //获取滚动条到左边的垂直宽度
相关推荐
- 浏览器是如何一步步把代码变成网页的? - Web前端
- 细说浏览器特性检测(2)-通用事件检测 - Web前端
- 完美兼容各大浏览器的jQuery插件实现图片切换特效 - Web前端
- jquery实现不同大小浏览器使用不同的css样式表的方法 - Web前端
- jqGrid随窗口大小变化自适应大小的示例代码 - Web前端
- jquery获取div距离窗口和父级dv的距离示例 - Web前端
- jquery 清空file域示例(兼容个浏览器) - Web前端
- jquery DIV撑大让滚动条滚到最底部代码 - Web前端
- 基于jquery固定于顶部的导航响应浏览器滚动条事件 - Web前端
- 使用jQuery简单实现模拟浏览器搜索功能 - Web前端
- 网页前端技术排行
-
- 1[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 2分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 3jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 4【第六章】Foundation之按钮和下拉功能 - Web前端
- 5基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 6jQuery实现鼠标移到元素上动态提示消息框效果 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 9jQuery编写widget的一些技巧分享 - Web前端
- 最近发表
-
- WordPress 应用:如何修改模版头部文件中的 wp_head() - Web前端
- SEO 优化:YouTube SEO Plugin 提升影片流量 - Web前端
- 使用 React 服务器组件在 Next.js 13 中进行国际化 - Web前端
- 3 个意想不到的技术 SEO 问题示例(以及如何解决它们) - Web前端
- Vue 技术专题:Suspense、路由模式与单元测试实践 - Web前端
- 如何防止 WordPress SQL 注入攻击? - Web前端
- SEO 优化:如何围绕技术SEO框架以构建和制定策略? - Web前端
- SEO优化:YouTube SEO 关键词研究工具 - Web前端
- 2025 年帮助代理机构提升客户业绩的 14 款最佳 SEO 工具 - Web前端
- 客户端:服务器之间设置本地 DNS 服务器 - Web前端


