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

dom元素各种高度

时间:2023-03-26 20:51:23 JavaScript

获取body滚动高度环境:chrome浏览器有h5文档声明:document.body.scrollTop在Standard模式下始终为0document.documentElement.scrollTop,window.pageYOffset正确无文档声明document.body.scrollTop获取正确的document.documentElement.scrollTop始终为0window.pageYOffset获取正确的兼容写法:letheight=document.body.scrollTop||区别//示例屏幕宽度414,div宽度100%

//p-padding,b-border,h-height,w-width1.heightdocument.querySelector('#out').clientHeight340300(h)+20(p)*2document.querySelector('#out').offsetHeight360300(h)+20(p)*2+10(h)*2文档。querySelector('#out').scrollHeight540500(h)+20(p)*22.widthdocument.querySelector('#out').clientWidth394414(w)-10(b))*2document.querySelector('#out').offsetWidth414414(w)document.querySelector('#out').scrollWidth394414(w)-10(b)*23.top向下滚动内必须100pxdocument.querySelector('#out').clientTop10边框10document.querySelector('#out').offsetTop100边距100document.querySelector('#out').scrollTop100滚动100document.querySelector('#inner').clientTop00document.querySelector('#inner').offsetTop130100+20+10document.querySelector('#inner').scrollTop0不滚动3.left向下滚动inner必须是100pxdocument.querySelector('#out').clientLeft10border10document.querySelector('#out').offsetLeft0document.querySelector('#out').scrollLeft0summaryclientHeight元素高度(不包括boder)offsetHeight元素高度(包括boder)scrollHeight滚动元素总高度scrollTop滚动高度(隐藏部分)clientTop上边框的高度offsetTop相对于offsetParent的高度offsetParent:最近的祖先元素(relative,absolute,fixed)带定位,如果祖先不满足条件,offsetParent就是body。当dom元素为doument.body或document.documentElement时,clientHeight等于屏幕高度。