使用javascript更改css定位
这段代码和我预期的一样完美,但问题是当我刷新页面时,t_con保持为绝对值,这应该是固定的,当我将窗口调整到小于980px时,它是绝对值,就像我需要的一样,如果我再次调整大小超过980px,它就会被固定 我需要它从一开始就保持固定。我不知道为什么一开始它是绝对的 HTML代码使用javascript更改css定位,javascript,css,css-position,Javascript,Css,Css Position,这段代码和我预期的一样完美,但问题是当我刷新页面时,t_con保持为绝对值,这应该是固定的,当我将窗口调整到小于980px时,它是绝对值,就像我需要的一样,如果我再次调整大小超过980px,它就会被固定 我需要它从一开始就保持固定。我不知道为什么一开始它是绝对的 HTML代码 <div id="t_con"> <div id="t"> </div> </div> Javascript代码 window.onresize = function()
<div id="t_con">
<div id="t">
</div>
</div>
Javascript代码
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";
}
}
window.onresize=function(){
var pos=窗宽<980?绝对值:'固定';
document.getElementById('t_-con')。style.position=pos;
}window.onresize=function(){
var header=document.getElementById('t');
var window_width=document.body.offsetWidth;
如果(窗宽<980){
header.style.position=“绝对”;
}否则{
header.style.position=“固定”;
}
}
谁能告诉我这里做错了什么。我不想用jQuery来处理这个问题。
所以请不要建议。我不喜欢jQuery的原因是它已经需要90kb加上我们试图执行的代码。window.onresize=function(){
window.onresize = function(){
var pos = window.outerWidth < 980 ? 'absolute' : 'fixed';
document.getElementById('t_con').style.position = pos;
}
var pos=窗宽<980?绝对值:'固定';
document.getElementById('t_-con')。style.position=pos;
}
window.onresize=function(){
var pos=窗宽<980?绝对值:'固定';
document.getElementById('t_-con')。style.position=pos;
}
window.onresize=function(){
var header=document.getElementByID('t_con');
var window_width=document.body.offsetWidth;
如果(窗宽<980){
header.style.position=“绝对”;
}否则{
header.style.position=“固定”;
}
}
window.onresize=function(){
var header=document.getElementByID('t_con');
var window_width=document.body.offsetWidth;
如果(窗宽<980){
header.style.position=“绝对”;
}否则{
header.style.position=“固定”;
}
}
t\u con.innerWidth
到底是什么t\u con
,你把它命名为标题
,你可能应该检查窗口
,对吗?我需要更改它???/得到了答案伙计们。。。我在代码中做了一次尝试,adeneo向我指出了这一点。然后尽可能地接受答案。t\u-con.innerWidth
到底是什么t\u-con
,您将其命名为header
,您可能应该检查窗口,对吗?我需要更改它???/得到了答案伙计们。。。我对代码进行了修改,adeneo向我指出了这一点。如果可以,请随意接受答案。如果我调整窗口大小,它不会变为绝对值。它保持不变。你有一个拼写错误,是getElementById,最后一个d用小写字母表示。哦,谢谢你,我是个白痴,我犯了这个简单的错误,差点因为不工作而哭。非常感谢。请重新阅读我的问题@adeneo如果我调整窗口大小,它不会变为绝对值。它保持不变。你有一个拼写错误,是getElementById,最后一个d用小写字母表示。哦,谢谢你,我是个白痴,我犯了这个简单的错误,差点因为不工作而哭。非常感谢。你能重新看我的问题吗请@adeneo你能重新看我的问题吗请@adidican你能重新看我的问题吗请@adidi
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_con');
var window_width = document.body.offsetWidth;
if(window_width < 980){
header.style.position="absolute";
}else{
header.style.position="fixed";
}
}