当前位置: 首页 > Web前端 > CSS

JS动态解析浏览器和网页的各种宽高属性

时间:2023-03-31 11:01:56 CSS

还有获取各种宽高属性的公式body{padding:50px;高度:500px;边框:1px红色虚线;溢出:滚动;}span{颜色:蓝色;}

网页可见区域的高度document.body.clientHeight=height+padding*2-滚动条宽度测试文字12
网页document.body可见区域的宽度.clientWidth=width+padding*2-滚动条宽度测试文字12
body总高度document.body.offsetHeight=height+border*2+padding*2=clientHeight+scrollbarwidth+borderwidth*2测试文字12
body总宽度document.body.offsetWidth=width+border*2+padding*2=clientWidth+scrollbarwidth+borderwidth*2测试文字12
scrollHeight的MDN解释:元素内容高度的度量,包括Invisiblecontentinviewduetooverflow
Totalscrollheightdocument.body.scrollHeighttesttext12
scrollheightdocument.body.scrollToptesttext12
scroll文档总宽度.body.scrollWidth测试文本12
浏览器可见窗口的高度,不包括边框、工具栏和调试窗口(可变)window.innerHeight测试文本12
浏览器可见窗口的宽度,不包括边框(可变)window.innerWidth测试文本12
浏览器窗口宽度,包括滚动条和边框(可变)window.outerHeighttestedtext12
高度浏览器窗口,包括边框、工具栏(可变)window.outerWidthtesttext12
屏幕物理分辨率高(不变)window.screen.height=window.screen.availHeight+windows上下任务栏测试文本12
屏幕物理分辨率宽(不变)window.screen.width=window.screen.availHeight+windows左右任务栏测试文字12
浏览器窗口可用高度,不包括windowstasksBar(variable)window.screen.availHeightTestedtext12
浏览器窗口的可用宽度,不包括windowstaskbar(variable)window.screen.availWidth测试文本12
浏览器窗口距离显示顶部的高度(可变)window.screenTop测试文字12
浏览器窗口距显示屏幕底部的高度(可变)window.screenLeft测试文字12
下面我们动态监听它们的变化,添加各种监听事件,监听浏览器大小变化时各种数据的变化规律functionbody_scroll(){console.log(this)console.log(document.body.scrollTop)}window.onload=function(){dataUpdate();}window.onresize=function(){dataUpdate();}window.onscroll=function(){控制台。日志(“window.onscroll”)数据更新();}document.body.onclick=function(){dataUpdate();}//浏览器位置改变了之后我们点击body来获取改变使用的原生JS来获取值functiondataUpdate(){varclient_height=document.getElementById("span_client_height");client_height.innerText=document.body.clientHeight||document.documentElement.clientHeight;varclient_width=document.getElementById("span_client_width");client_width.innerText=document.body.clientWidth||document.documentElement.clientWidth;varclient_offsetHeight=document.getElementById("span_client_offsetHeight");client_offsetHeight.innerText=document.body.offsetHeight;varclient_offsetWidth=document.getElementById("span_client_offsetWidth");client_offsetWidth.innerText=document.body.offsetWidth;varclient_scrollHeight=document.getElementById("span_client_scrollHeight");client_scrollHeight.innerText=document.body.scroll身高;varclient_scrollTop=document.getElementById("span_client_scrollTop");client_scrollTop.innerText=document.body.scrollTop;varclient_scrollWidth=document.getElementById("span_client_scrollWidth");client_scrollWidth.innerText=document.body.scrollWidth;varwindow_innerHeight=document.getElementById("span_inner_height");window_innerHeight.innerText=window.innerHeight;varwindow_innerWidth=document.getElementById("span_inner_width");window_innerWidth.innerText=window.innerWidth;varwindow_outerHeight=document.getElementById("span_outer_height");window_outerHeight.innerText=window.outerHeight;varwindow_outerWidth=document.getElementById("span_outer_width");window_outerWidth.innerText=window.outerWidth;varscreen_height=document.getElementById("span_screen_height");screen_height.innerText=window.screen.hei正确;varscreen_width=document.getElementById("span_screen_width");screen_width.innerText=window.screen.width;varscreen_availHeight=document.getElementById("span_screen_availHeight");screen_availHeight.innerText=window.screen.availHeight;varscreen_availWidth=document.getElementById("span_screen_availWidth");screen_availWidth.innerText=window.screen.availWidth;varwindow_top=document.getElementById("span_window_top")window_top.innerText=window.screenTopvarwindow_left=document.getElementById("span_window_left")window_left.innerText=window.screenLeft}我们可以进一步封装,写一个页面宽高检测器使用策略模式。你可以查看http://jianjiacheng.com/brows...