Ryan
2007-12-27, 21:52
I'm trying to change the class of an element of a <div> based on whether content fills the viewport or not. Ideally, I would just be able to change the position attribute directly, but I couldn't get that to work, so I settled for changing class.
Here's the HTML:
<div id="footer" class="relative">
<script language="javascript">
var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ?
document.documentElement.clientHeight : document.body.clientHeight;
var divh = document.getElementById('container').offsetHeight;
if(divh < windowHeight) {
document.getElementById('footer').className = 'absolute';
}
else {
document.getElementById('footer').className = 'relative';
}
</script>
//more HTML
</div>I'm going to remove the Javascript from the HTML after I get it working.
The CSS:
#footer {
bottom: 0;
padding: 0 0 20px 0;
float: none;
clear: both;
margin: auto auto 0 80px;
width: 540px;
height: 4em;
text-align: left;
font-size: 8pt;
color: rgb(181,181,181);
}
.relative {
position: relative;
}
.absolute {
position: absolute;
}
It works in Safari, but not Firefox. I haven't even tried IE yet (that'll be fun...). Any ideas?
Here's the HTML:
<div id="footer" class="relative">
<script language="javascript">
var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ?
document.documentElement.clientHeight : document.body.clientHeight;
var divh = document.getElementById('container').offsetHeight;
if(divh < windowHeight) {
document.getElementById('footer').className = 'absolute';
}
else {
document.getElementById('footer').className = 'relative';
}
</script>
//more HTML
</div>I'm going to remove the Javascript from the HTML after I get it working.
The CSS:
#footer {
bottom: 0;
padding: 0 0 20px 0;
float: none;
clear: both;
margin: auto auto 0 80px;
width: 540px;
height: 4em;
text-align: left;
font-size: 8pt;
color: rgb(181,181,181);
}
.relative {
position: relative;
}
.absolute {
position: absolute;
}
It works in Safari, but not Firefox. I haven't even tried IE yet (that'll be fun...). Any ideas?