I am writing a bit of Javascript code into pages that prepends a div to the body that is supposed to remain at the top of the document or window, regardless of what design/content the page has, and was working fine until came across a header element fixed top.
What I'm trying to accomplish with CSS/JS only, is to keep the jsbar at the top no matter what. If there's elements on the page such as in the example, the jsbar should be stacked above the element(s) not over or under like layers. In other words, if there is something fixed top:0, shift it down the height of jsbar, making the new top=0+heightofjsbar for all other elements.
See raw basic example of the problem: http://jsfiddle.net/XEZ5R/
<div class="jsbar">This is dynamically created jsbar</div>
<div class="page">
<header>The header bar</header>
<div class="content">the content here </div>
</div>
.page {
position:relative;
top:0;
background:#f8f8f8;
height:900px;
}
header {
position:fixed;
top:0;
background:#eee;
height:10px;
}
.jsbar {
position:fixed;
top:0;
background:#ccc;
height:10px;
}
I would appreciate any ideas to fix this. The less I tamper with the page the better. The jsbar is written into all kinds of websites with all kinds of designs/elements.
Thank you.
UPDATE: Here is an example of the desired result: http://jsfiddle.net/Zzat9/1/ (view result, disregard html/css)
Also... I can only use Javascript, no jQuery, and I only have control of the Javascript and CSS that I output/prepend at the top of the document... I have no control over anything else. So for the examples here... consider everything other than jsbar as the owners page/content/elements.
My thinking is it's going to take some Javascript that will look for elements that have position:fixed or absolute relative to the document, and then move them down the height of the jsbar. Something like: how to query whole DOM for elements matching some computed style ? (in pure js)
UPDATE 2:
I have come up with this bit Javascript that does what is needed:
var docelements = document.querySelectorAll('header, div, section');
var jsbarheight = '25px';
for (var i=0, max=docelements.length; i < max; i++)
{
if(window.getComputedStyle(docelements[i],null).getPropertyValue('position')=='fixed')
{
var fixedelement = docelements[i];
if(window.getComputedStyle(fixedelement,null).getPropertyValue('top')=='0px')
{
fixedelement.style.top=jsbarheight;
document.body.style.paddingTop=jsbarheight;
}
}
}
But is this the most efficient? Anyone see any issues with this? Immediate downside is the delay in which the adjustments are made.