BillR
09-03-2008, 09:25 PM
I found a javascript that someone had put together (I have been looking in many places and don't know who made it) that will open a div 200px down the screen from the top of the browser window. This only has four links. My page currently has a growing number of links that open on a new page and 14 links that I open on the same page and want them to open down 200px. Here is the javascript that I found:
<script type="text/javascript">
var def=0; // Default padding of your divs
window.onload=function()
{
var links=document.getElementsByTagName('a'),
nos=4, // Amount of your links. This is important for the loop of our function.
baselink='room', // Prefix of your anchor
distance='200px'; // Distance you wish when the anchor is choosen
links[0].onclick=function()
{reset();divs=baselink+1;
document.getElementById(divs).style.paddingTop=distance;
this.href='#'+divs;
}
links[1].onclick=function()
{reset();divs=baselink+2;
document.getElementById(divs).style.paddingTop=distance;
this.href='#'+divs;
}
links[2].onclick=function()
{reset();divs=baselink+3;
document.getElementById(divs).style.paddingTop=distance;
this.href='#'+divs;
}
links[3].onclick=function()
{reset();divs=baselink+4;
document.getElementById(divs).style.paddingTop=distance;
this.href='#'+divs;
}
}
function reset()
{
var divs=document.getElementsByTagName('div');
for(var i=0;i<divs.length;i++){divs[i].style.paddingTop=def+'px';}
}
</script>
The HTML that uses THIS code is:
<a href="#">Room1</a>
<a href="#">Room2</a>
<a href="#">Room3</a>
<a href="#">Room4</a>
<div id="room1" style="height:500px;">
Some Content div 1
</div>
<div id="room2" style="height:500px;">
Some Content div 2
</div>
<div id="room3" style="height:500px;">
Some Content div 3
</div>
<div id="room4" style="height:500px;">
Some Content div 4
</div>
I tested this and it worked, then I tried to modify it, and my modification does not work. Nothing happens to modify the link in my code. You can see it in action (actually no action) at: http://www.roberson-family.com/testing/test_offset.html
Please help this NOVICE understand a little javascript.
<script type="text/javascript">
var def=0; // Default padding of your divs
window.onload=function()
{
var links=document.getElementsByTagName('a'),
nos=4, // Amount of your links. This is important for the loop of our function.
baselink='room', // Prefix of your anchor
distance='200px'; // Distance you wish when the anchor is choosen
links[0].onclick=function()
{reset();divs=baselink+1;
document.getElementById(divs).style.paddingTop=distance;
this.href='#'+divs;
}
links[1].onclick=function()
{reset();divs=baselink+2;
document.getElementById(divs).style.paddingTop=distance;
this.href='#'+divs;
}
links[2].onclick=function()
{reset();divs=baselink+3;
document.getElementById(divs).style.paddingTop=distance;
this.href='#'+divs;
}
links[3].onclick=function()
{reset();divs=baselink+4;
document.getElementById(divs).style.paddingTop=distance;
this.href='#'+divs;
}
}
function reset()
{
var divs=document.getElementsByTagName('div');
for(var i=0;i<divs.length;i++){divs[i].style.paddingTop=def+'px';}
}
</script>
The HTML that uses THIS code is:
<a href="#">Room1</a>
<a href="#">Room2</a>
<a href="#">Room3</a>
<a href="#">Room4</a>
<div id="room1" style="height:500px;">
Some Content div 1
</div>
<div id="room2" style="height:500px;">
Some Content div 2
</div>
<div id="room3" style="height:500px;">
Some Content div 3
</div>
<div id="room4" style="height:500px;">
Some Content div 4
</div>
I tested this and it worked, then I tried to modify it, and my modification does not work. Nothing happens to modify the link in my code. You can see it in action (actually no action) at: http://www.roberson-family.com/testing/test_offset.html
Please help this NOVICE understand a little javascript.