getTOP()
ブラウザ(または親要素)左端からのレイヤ−左辺位置をピクセルで取得するためのサンプルファンクションです。
n4とm1,n6,n7,s1では
top、
e4,e5,e6,o6,o7では
pixelTopを使っています。
注:m1,n6,n7,s1(e5,e6もtopで処理した場合)などではインラインスタイルまたはスクリプトで
あらかじめ値を指定していない限り初期値は空白です。
*このファンクションをコピーして<script>と</script>の間にペーストしておくだけでこの機能をクロスブラウザに利用できるようになります。
Cross-Browser のための Sample Function
//--レイヤ−名で処理する場合
Syntax : getTOP('レイヤ−名')
function getTOP(layName){
if(document.all) //e4,e5,e6,o6,o7用
return document.all(layName).style.pixelTop
else if(document.getElementById) //n6,n7,m1,s1用
return (document.getElementById(layName).style.top!="")
?parseInt(document.getElementById(layName).style.top):""
else if(document.layers) //n4用
return document.layers[layName].top
}
Example
<script type='text/javascript'>
<!--
function getTOP(layName){
if(document.all) //e4,e5,e6,o6,o7用
return document.all(layName).style.pixelTop
else if(document.getElementById) //n6,n7,m1,s1用
return (document.getElementById(layName).style.top!="")
?parseInt(document.getElementById(layName).style.top):""
else if(document.layers) //n4用
return document.layers[layName].top
}
//-->
</script>
<!--↓このリンクをクリックすると上辺の位置がわかります/////-->
<div id="test0"
style="position : absolute ;
left : 100px ;
top : 80px ;
width : 300px ;
height : 50px ;
clip : rect(0,300,50,0) ;
font-size : 12pt ;">
<a href="javascript:alert(getTOP('test0'))">
このレイヤーの上辺の位置は<br>
ブラウザ上端から何ピクセル?</a>
</div>
//--オブジェクト名で処理する場合
上記の関数をレイヤー名の代わりにオブジェクトで処理するようにしたもの。
ブラウザ毎の実装オブジェクト処理が別立てになるので関数がシンプルになる。
Syntax : getTOP(object)
function getTOPoj(oj){
if(document.all) //e4,e5,e6,o6,o7用
return oj.pixelTop
else if(document.getElementById) //n6,n7,m1,s1用
return (oj.top!="")?parseInt(oj.top):""
else if(document.layers) //n4用
return oj.top
}
Example
<script type='text/javascript'>
<!--
function getTOPoj(oj){
if(document.all) //e4,e5,e6,o6,o7用
return oj.pixelTop
else if(document.getElementById) //n6,n7,m1,s1用
return (oj.top!="")?parseInt(oj.top):""
else if(document.layers) //n4用
return oj.top
}
//--layNameで指定したオブジェクトを返す(必ずonload後に実行すること)
function getLayStyleOj(layName){
if(document.getElementById) //e5,e6,n6,n7,m1,o6,o7,s1用
return document.getElementById(layName).style
else if(document.all) //e4用
return document.all(layName).style
else if(document.layers) //n4用
return document.layers[layName]
}
//-->
</script>
<!--↓このリンクをクリックすると上辺の位置がわかります/////-->
<div id="test0"
style="position : absolute ;
left : 100px ;
top : 80px ;
width : 300px ;
height : 50px ;
clip : rect(0,300,50,0) ;
font-size : 12pt ;">
<a href="javascript:alert(getTOP(getLayStyleOj('test0')))">
このレイヤーの上辺の位置は<br>
ブラウザ上端から何ピクセル?</a>
</div>