0

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.

4
  • t_con.innerWidth what the heck is t_con, you named it header, and you should probably check the window, right ? Commented Apr 13, 2013 at 1:08
  • i need to change it??/ Commented Apr 13, 2013 at 1:09
  • Got the answer guys... I made amistake in the code and it was noted to me by adeneo. Commented Apr 13, 2013 at 1:23
  • Then feel free to accept the answer when you can. Commented Apr 13, 2013 at 1:24

2 Answers 2

4
window.onresize = function(){
    var pos = window.outerWidth < 980 ? 'absolute' : 'fixed';
    document.getElementById('t_con').style.position = pos;
}

FIDDLE

Sign up to request clarification or add additional context in comments.

4 Comments

if i resize the window it is not changing to absolute. it stays fixed.
You had a typo, it's getElementById, with the last d in lowercase.
oh thank you i am an idiot i made this simple mistake and was about to cry for not working. thank you so much.
can you relook my question pls @adeneo
0
window.onresize=function(){
var header=document.getElementByID('t_con');
var window_width = document.body.offsetWidth;
if(window_width < 980){
    header.style.position="absolute";
}else{
    header.style.position="fixed";
}
}

1 Comment

can you relook my question pls @adidi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.