使用 Javascript 更改 div 标签的绝对位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8025189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:25:21  来源:igfitidea点击:

Change the absolute position of a div tag with Javascript

javascript

提问by Crystal

I have a div tag declared like this:

我有一个像这样声明的 div 标签:

<div id="carRight" style="position:absolute; left:600px; top:170px;">
<img src="carRight.png" width="256" height="256" />
</div>

After a quick animation, I'd like to move the left position when a button is pressed. I tried a few variations of this, but it doesn't seem to be correct:

快速动画后,我想在按下按钮时移动左侧位置。我尝试了一些变体,但似乎不正确:

document.getElementById('carRight').style.left=600;
document.getElementById('carRight').style.position.left=600;

What is the correct statement to use? Thanks.

要使用的正确语句是什么?谢谢。

回答by NJLaPrell

Try:

尝试:

document.getElementById('carRight').style.left="600px";

回答by bensiu

document.getElementById('carRight').style="position:absolute; left:100px; top:170px;";

document.getElementById('carRight').style="position:absolute; left:100px; top:170px;";

where 100pxis new left position... position:absolute;part could be moved to class...

100px新的左边位置在哪里……position:absolute;部分可以移到课堂上……

回答by shreyas

It is not essential to write element.style.left="600px";. Just add document.getElementById('carRight').position='relative';.

没有必要写element.style.left="600px";。只需添加 document.getElementById('carRight').position='relative';.