<html>
<head>
<title>www.tutorials.de</title>
<meta name="author" content="Quaese">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
#Absatz{
height: 300px;
overflow: auto;
background: #efefef;
}
#Absatz p{
padding: 0;
margin: 0;
height: 30px;
}
//-->
</style>
<script type="text/javascript">
function Test(strId){
var objOuter = document.getElementById("Absatz");
var objInner = document.getElementById(strId);
// Top-Offsets ermitteln
var objOffset = {
outer: getTopOffset(objOuter),
inner: getTopOffset(objInner)
};
// Scroll-Höhe zuweisen (= Differenz der beiden Elemente)
objOuter.scrollTop = (objOffset.inner-objOffset.outer);
}
// Funktion zum Ermitteln des Top-Offsets eines übergebenen Elements
function getTopOffset(objTest){
var intOffset = objTest.offsetTop;
var objParent = objTest.offsetParent;
while(objParent){
intOffset += objParent.offsetTop;
objParent = objParent.offsetParent;
}
return intOffset;
}
</script>
</head>
<body>
<h1>Hallo Welt</h1>
<div id="Absatz">
<script type="text/javascript">
for(var i=1; i<100; i++){
strInsert = (i%2==0)? " style=\"background: #efef00;\"" : "";
document.write("<p id=\"zeile"+i+"\""+strInsert+">Zeile " + i + "</p>");
}
</script>
</div>
<div style="height: 1200px;">Höhen-Element</div>
<p><a href="javascript:Test('zeile50')">Test</a></p>
</body></html>