This code works perfectly as i expected but the problem is when i refresh the page the t_con stays in absolute which should be fixed and when i resize the window less than 980px it is in absolute like what i need and if i resize again more than 980px it goes fixed.
i need it to stay fixed from start. I dont know why it is in absolute at first.
HTML CODE
<div id="t_con">
<div id="t">
</div>
</div>
CSS CODE
#t_con{
width:100%;
position:absolute;
top:0;
}
#t{
margin:0px auto 0 auto;
background-color:#976398;
width:980px;
height:30px;
}
Javascript CODE
window.onresize = function(){
var pos = window.outerWidth < 980 ? 'absolute' : 'fixed';
document.getElementById('t_con').style.position = pos;
}window.onresize=function(){
var header=document.getElementById('t');
var window_width = document.body.offsetWidth;
if(window_width < 980){
header.style.position="absolute";
}else{
header.style.position="fixed";
}
}
Can anyone tell what i am doing wrong in here. And i don't want to use jQuery for this. So pls dont suggest it. The reason i dont like jQuery is it already takes 90kb plus the code we are trying to execute.
t_con.innerWidthwhat the heck ist_con, you named itheader, and you should probably check thewindow, right ?