2008年12月1日月曜日

エレメントのx,y位置を取得する関数

つづいて、エレメントのx,y位置を取得する関数です。


cc_huu_tools_bp_20081024.getX = function(e) {
  var x = 0;
  while(e) {
    x += e.offsetLeft;
    e = e.offsetParent;
  }
  return x;
};

cc_huu_tools_bp_20081024.getY = function(e) {
  var y = 0;
  while(e) {
    y += e.offsetTop;
    e = e.offsetParent;
  }
  return y;
};


順に親ノードをたどり、x,y位置を足し込んでいって、エレメントのx,y位置を計算します。

0 件のコメント: