Using Javascript to relocate text -...

User 355448 Photo


Ambassador
3,144 posts

The page I am working on has a top and bottom that are fixed and also have a high z-index. The middle can scroll and has a low z-index, allowing it to disappear below the top and bottom sections.

The problem I am having is that when I click on an anchor to move down the middle section, the anchored text is hidden behind the top section. I found some javascript that should really help with this problem, but I can't get it to work when I modify it for my page.

Here is the code that works:
<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 the javascript 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>

The page with the modified code is http://www.roberson-family.com/testing/test_offset.html and it acts as if the javascript is not working at all.

Learning javascript is on my short list of things to get done.
User 146685 Photo


Registered User
11 posts

billr,

Just tried going to the link = Internal Server Error and even the main URL and got the same thing..

http://www.roberson-family.com/

Without seeing anything...hard to troubleshoot.

Jim
User 355448 Photo


Ambassador
3,144 posts

Jim,

You were trying to get to my site as I was trying to fix another issue. My fix resulted in the internal server error, and dwhen I realized it was affecting all my site, I removed my "fix" which shows why a beginner like me should be careful.

The site is accessible now.
User 463058 Photo


Ambassador
1,086 posts

Hi Bill,

I don't know where you're at with this stuff, but here's a newer and better approach figured out by the same CSS genius, Paul O'Brien, who came up with the code I pointed you to before. This code works in standards mode (it doesn't need to throw IE into quirks mode like the previous versions), it's simpler, and it can handle your anchors!

http://www.pmob.co.uk/temp/fixed-header-footerx1.htm

Your page with this code applied (I omitted the main table listing):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Names of the Roberson Family</title>
<meta name="DESCRIPTION" content="Alphabetical list of surnames on the Roberson family genealogy site." />
<meta name="keywords" content="ancestory, family tree,Roberson Family" />
<link rel="stylesheet" href="../style/genealogy.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="author" content="Bill Roberson" />
<meta name="contact" content="webmaster@roberson-family.com" />
<link rel="shortcut icon" href="http://www.roberson-family.com/img/favicon.ico" type="image/x-icon" />
<link rel="icon" type="image/ico" href="/img/favicon.ico" />
<style type="text/css">
* {margin:0;padding:0;}
p {margin:0 0 1em 0}
html, body {margin:0;padding:0;}
body{height:100%;margin:0;}

#middle {
overflow:auto;
position:absolute;
left:0;
right:0;
top:180px;
bottom:80px;
}

#top {
position:absolute;
left:0;
right:0;
top:0;
height:180px;
text-align:center;
background-image:url('../img/gen/niceblue.gif');
overflow:hidden;
}

#bottom {
position:absolute;
left:0;
right:0;
bottom:0;
height:80px;
text-align:center;
background-image:url('../img/gen/niceblue.gif');
overflow:hidden;
}
</style>
<!--[if IE ]>
<style type="text/css">

* html {padding:180px 0 80px 0;overflow:hidden}
* html body{padding:180px 0 80px 0;padd\ing:0;overflow:hidden}
* html #middle{
height:100%;
position:static;
}
* html #top{
width:100%;
left:0;
}
* html #bottom{
left:0;
width:100%;
}
</style>
<![endif]-->
<style type="text/css">
#alpha {
width:720px;
text-align:center;
margin-left: auto;
margin-right: auto;
}
table.name {
width:720px;
padding:0;
margin:0;
text-align:left;
}
#alpha td {
/*width:230px;*/
padding:0;
margin:0;
white-space: nowrap;
}
#alpha tr {
/*width:710px;*/
text-align:left;
padding:0;
margin:0;
}
th.names {
background-color:transparent;
color:black;
text-align:left;
width:240px;
padding:0;
margin:0;
}
td.surname {
font-weight:bold;
width:33%;
}
#alpha td.person {
width:33%;
font-size:medium;
text-align:left;
padding-left: 10px;
}
#alpha .suname a {
position: relative;
top: 500px;
}
h3 {
margin-top:0;
padding-top:0;
margin-bottom:0;
}
p.alphalink {
margin-top:0;
padding-top:0;
font-size:medium;
margin-bottom:0;
}
table.test01 {
width:770px;
margin-left:auto;
margin-right:auto;
}
</style>
</head>
<body>
<div id="top">
<div id="mast">
<a href="http://www.roberson-family.com"><img src="../img/family4.png" width="200" height="50" alt="Roberson Family" title="Roberson Family" /></a><br />GENEALOGY
</div><!-- close mast div -->
<h3>Name List</h3>
<p class="alphalink"><a href="#aa">No Surname</a>, A, <a href="#b">B</a>, <a href="#c">C</a>, <a href="#d">D</a>, E, F, <a href="#g">G</a>, <a href="#h">H</a>, I, <a href="#j">J</a>, <a href="#k">K</a>, <a href="#l">L</a>, <a href="#m">M</a>, N, O, P, Q, <a href="#r">R</a>, <a href="#s">S</a>, <a href="#t">T</a>, U, <a href="#v">V</a>, W, X, Y, Z, </p>
<table class="test01" style="text-align:center;">
<tr>
<td width="*">&nbsp;</td>
<th class="names"><h2>Name</h2></th>
<th class="names"><h2>Born</h2></th>
<th class="names"><h2>Died</h2></th>
<td width="*">&nbsp;</td>
</tr>
</table>
</div><!-- close the fixed top div -->
<div id="middle">
<div id="namedisplay">
<div id="alpha">
...Your main table here...
</div><!-- close alpha div -->
</div><!-- close namedisplay div -->
</div>
<!-- close the scrolling middle div -->
<div id="bottom">
<div id="footer">
<p><a href="/genealogy/g_index.php" target="_top">Table of Contents</a> | <a href="/genealogy/surnames.php" target="_top">Surnames</a> | <a href="/genealogy/names_frame.php" target="_top">Name List</a></p>
<p class="xxs">This Website was Created Jul 12, 2008 with <a href="http://www.coffeecup.com/html-editor/" target="_top">HTML Editor 2008</a> from <a href="http://www.coffeecup.com" target="_top">CoffeeCup Software</a></p>
</div><!-- close footer div -->
</div><!-- close the fixed bottom div -->
</body>
</html>
User 355448 Photo


Ambassador
3,144 posts

Cary,

I have copied that code and ...

WOW!!

That did the job.

THANK YOU!!!

Now all I need to do is finish putting all the information on that page.

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.