本文详细解析JavaScript中scrollLeft、scrollWidth、clientWidth和offsetWidth等核心DOM属性的定义与用法。重点对比了IE6.0、FF1.06+以及IE5.0/5.5在不同浏览器版本中对这些属性的计算逻辑差异,包括padding和border的影响。同时说明了event.clientX、offsetX等坐标属性与滚动条位置的关系,帮助开发者解决跨浏览器布局兼容性问题。
滚动与偏移属性定义
scrollHeight属性用于获取元素内部内容的实际滚动高度。
scrollLeft属性用于设置或获取元素左边界与窗口中当前可见内容最左端之间的距离。
scrollTop属性用于设置或获取元素最顶端与窗口中当前可见内容最顶端之间的距离。
scrollWidth属性用于获取元素内部内容的实际滚动宽度。
offsetHeight属性用于获取元素相对于页面或由offsetParent属性指定的父坐标的高度。
offsetLeft属性用于获取元素相对于页面或由offsetParent属性指定的父坐标的计算左侧位置。
offsetTop属性用于获取元素相对于页面或由offsetParent属性指定的父坐标的计算顶端位置。
鼠标事件坐标与滚动计算
event.clientX表示鼠标事件相对于文档的水平坐标。
event.clientY表示鼠标事件相对于文档的垂直坐标。
event.offsetX表示鼠标事件相对于容器的水平坐标。
event.offsetY表示鼠标事件相对于容器的垂直坐标。
document.documentElement.scrollTop用于获取垂直方向的滚动距离值。
event.clientX加上document.documentElement.scrollTop的值,表示相对于文档的水平坐标加上垂直方向的滚动量。
不同浏览器版本的布局差异
上述描述主要基于IE浏览器的行为,Firefox浏览器存在以下差异:
在IE6.0及FF1.06+版本中:
clientWidth等于内容宽度width加上内边距padding。
clientHeight等于内容高度height加上内边距padding。
offsetWidth等于内容宽度width加上内边距padding再加上边框border。
offsetHeight等于内容高度height加上内边距padding再加上边框border。
在IE5.0和IE5.5版本中:
clientWidth等于内容宽度width减去边框border。
clientHeight等于内容高度height减去边框border。
offsetWidth等于内容宽度width。
offsetHeight等于内容高度height。
需要注意的是,CSS中的margin属性与clientWidth、offsetWidth、clientHeight以及offsetHeight均无关联。
代码测试示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312"> <head> <head> <title> 代码实例:关于clientWidth、offsetWidth、clientHeight、offsetHeight的测试比较 </title> <meta http-equiv="content-type" content="text/html; charset=gb2312" /> <meta name="author" content="枫岩,CnLei.y.l@gmail.com"> <meta name="copyright" content="https://www.jb51.net" /> <meta name="description" content="关于clientWidth、offsetWidth、clientHeight、offsetHeight的测试比较" /> <style type="text/css" media="all"><!-- body {font-size:14px;} a,a:visited {color:#00f;} #Div_CnLei { width:300px; height:200px; padding:10px; border:10px solid #ccc; background:#eee; font-size:12px; } #Div_CnLei p {margin:0;padding:10px;background:#fff;} --></style><style type="text/css" media="all" bogus="1"> body {font-size:14px;} a,a:visited {color:#00f;} #Div_CnLei { width:300px; height:200px; padding:10px; border:10px solid #ccc; background:#eee; font-size:12px; } #Div_CnLei p {margin:0;padding:10px;background:#fff;} </style> </head> <body> <p>点击下面的链接:</p> <div id="Div_CnLei"> <p>GetClientWidth(); GetClientHeight();</p> <p>GetOffsetWidth(); GetOffsetHeight();</p> </div> <div id="Description"> <p><strong>IE6.0、FF1.06+:</strong> clientWidth = width + padding = 300+10×2 = 320 clientHeight = height + padding = 200+10×2 = 220 offsetWidth = width + padding + border = 300+10×2+10×2= 340 offsetHeight = height + padding + border = 200+10×2+10×2 = 240</p> <p><strong>IE5.0/5.5:</strong> clientWidth = width - border = 300-10×2 = 280 clientHeight = height - border = 200-10×2 = 180 offsetWidth = width = 300 offsetHeight = height = 200</p> </div> </body> </html> [Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]
