素人がプログラミングを勉強していたブログ

プログラミング、セキュリティ、英語、Webなどのブログ since 2008

連絡先: twitter: @javascripter にどうぞ。

getBoundingClientRect()を使って要素の絶対座標を求める


nowa 

 | Mozilla Developer Network使
offsetParent辿offsetWidth
function getElementPosition(elem){
  var position=elem.getBoundingClientRect();
  return {
    left:Math.round(window.scrollX+position.left),
    top:Math.round(window.scrollY+position.top)
  }
}

getElementPosition(document.links[5])

getBoundingClientRect()Math.round
lefttopwindow.scrollX,window.scrollY