我想将一个平时漂浮在右侧的div移动到一个绝对位置(由js决定),然后再回到初始位置。
第一部分已经可以工作并且依赖于为对象设置“style.”标签……类似
mytarget.style.position="absolute";
mytarget.style.top="50px";
要将其移回,我只想将标准类分配回元素,该元素还将包含定位信息。
奇怪的是,除了重新定位之外的所有方法都有效。 这是相关代码:
var mytarget= document.getElementById(titlebarid);
var newclass=tabstyleclasses[titlebarid][ismaxedtab[titlebarid]];
mytarget.className= newclass ;
调试:
alert ("wanting to set : " + newclass ) ;
这会输出正确的值。 (“adfieldtitlesm”)
alert ("my target is " +mytarget.id);
同样有效。
alert ("new class is " + mytarget.className );
也给出了正确的值。 (“adfieldtitlesm”)
alert ("new position is " + mytarget.style.position );
给出“absolute”(之前移动 mytarget 时由 JS 设置),应该是“fixed”
这是 CSS 类:
.adfieldtitlesm
{
position: fixed;
top: 200px;
left: 50px;
font-size: 50px;
background-color: #000000;
color: #ffffff;
}
为什么设置类后对象不移动?
更新:链接
与此相关的部分是 rebuildtabs() 中的第 270-297 行(主要是第 290-294 行),用于第一步,它有效 和 minwidget() 中的第 160-183 行 为稍微困惑的代码道歉:)
最佳答案
因为内联属性会覆盖类属性,所以您需要删除内联。
mytarget.style.position="";
那么类中定义的fixed
位置就起作用了。
或者可能使用:
delete mytarget.style.position;
不确定哪个是首选。
关于javascript - Div/CSS 位置在 javascript 操作后不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4834380/