dom.style.left 与 dom.offsetLeft区别

时间:2023-11-26 12:43:50

dom.style.left    初始空值,必须在html行内样式定义值才有值,在css样式定义仍为空值
                         可读写,是字符串,读写是必须加px,否则无效
                         js设置之后,有值

设置非整数值,会保持原样,例如 style.left = 8.22px, 输出style.left,仍然为8.22px

dom.offsetLeft  初始有值,只读,不可写,是数字

通过style.left设置的非整数值,会四舍五入,例如 style.left = 8.22px,属性offsetLeft, 会为 8

包括 content,padding,border的完整盒子宽度,使用时要注意

转换 dom.style.left = dom.offsetLeft + 'px'

style.top和offsetTop、style.width和offsetWidth、style.height和offsetHeight类似