Javascript JS-Changing.top属性
我有一个div,我想更改它的“top”属性。它当前设置在CSS表中,但是当我的函数完成时,我希望它改变 这是我试图使用的代码,但它没有任何作用:Javascript JS-Changing.top属性,javascript,css,Javascript,Css,我有一个div,我想更改它的“top”属性。它当前设置在CSS表中,但是当我的函数完成时,我希望它改变 这是我试图使用的代码,但它没有任何作用: <script> function reposition(){ var windowWidth = window.innerWidth; if (windowWidth > 667) { document.getElementById("Submi
<script>
function reposition(){
var windowWidth = window.innerWidth;
if (windowWidth > 667) {
document.getElementById("SubmitAFilm").style.top = "100px";
}
}
window.addEventListener("resize",changeFontSize,false);
</script>
函数重新定位(){
var windowWidth=window.innerWidth;
如果(窗口宽度>667){
document.getElementById(“SubmitAFilm”).style.top=“100px”;
}
}
window.addEventListener(“resize”,changeFontSize,false);
将您在addEventListener
中应用的函数更改为重新定位
。您还需要有position:absolute
,才能使用top
值
函数重新定位(){
var windowWidth=window.innerWidth;
如果(窗口宽度>667){
document.getElementById(“SubmitAFilm”).style.top=“100px”;
}
}
addEventListener(“调整大小”,重新定位,错误)代码>
#亚媒体电影{
位置:绝对位置;
}
提交电影
您应该在正在移动的div上设置position:absolute
下面是一个演示:
和示例代码:
<div id="SubmitAFilm" style="position:absolute">thefilm</div>
<script>
function reposition(){
var windowWidth = window.innerWidth;
if (windowWidth > 667) {
document.getElementById("SubmitAFilm").style.top = "100px";
console.log('changetop')
}
else {
document.getElementById("SubmitAFilm").style.top = "0px";
}
console.log('reposition')
}
window.addEventListener("resize",reposition,false);
</script>
电影
函数重新定位(){
var windowWidth=window.innerWidth;
如果(窗口宽度>667){
document.getElementById(“SubmitAFilm”).style.top=“100px”;
console.log('changetop')
}
否则{
document.getElementById(“SubmitAFilm”).style.top=“0px”;
}
console.log('重新定位')
}
addEventListener(“调整大小”,重新定位,错误);
编辑:第二种解决方案:如果不想使用绝对定位,可以在脚本中使用“marginTop”而不是“top”。结果将是您想要的。检查控制台是否有错误。你的代码中肯定有错误。你把这个函数叫哪里?哇。我刚刚注意到我犯了一个多么愚蠢的错误。我没有将侦听器从我的旧函数更改为“重新定位”:呸!发生在我们所有人身上:P您只需要检查控制台:)