Skip to content

Instantly share code, notes, and snippets.

@kawabataryo
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kawabataryo/20419e556150f69261d5 to your computer and use it in GitHub Desktop.
Save kawabataryo/20419e556150f69261d5 to your computer and use it in GitHub Desktop.
指定したIDの縦の位置を配列で返す関数
/**
* @param {Array} idAry 位置を取得したい要素のID名を配列で ※必須
* @param {Number} adjustPosition 位置を調整 [初期値=0]
*
* 取得したデータの呼び出し [インスタンス名].hashTalbe;
*/
function GetElementsOffsetTop(idAry,adjustPosition){
this.idAry = idAry;
this.len = this.idAry.length;
this.adjustPosition = adjustPosition || 0;
this.hashTable = this.generateObj();
}
GetElementsOffsetTop.prototype = {
//連想配列を生成
generateObj: function(){
var hashObj = {};
for(var i = 0; i < this.len; i++){
hashObj[this.idAry[i]] = this.getPosition(i) + this.adjustPosition;
}
return hashObj;
},
//要素の位置を取得
getPosition: function(i){
var el = document.getElementById(this.idAry[i]);
if(el){
return el.offsetTop;
}
}
}
@kawabataryo
Copy link
Author

window.onload = function(){
      //初期化
      var hoge = new GetElementsPosition(['hoge','fuga','fugo']);
      //配列の呼び出し
      console.log(hoge.hashTable);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment