2017-11-20 5 views
-2

this.elemento.style.topとthis.elemento.style.leftを編集するdivを移動しようとしています。javascript style.top/leftはdivで動作しません

あなたが見ることができるように、すべての最初はstyle.topとstyle.leftの値を取得し、コンソールに手が残しsttyle.top /の値を変更してみてください:

>>> this.elemento.style.top 
"48px" 
>>> this.elemento.style.left 
"18px" 
>>> this.elemento.style.top = "500px"; 
"500px" 
>>> this.elemento.style.left = "500px"; 
"500px" 

https://gyazo.com/090bb475a3affc117ea5c729a04b8cf3

this.elementoを使用すると、hmtlのdivに過ぎません。これは例です:https://gyazo.com/66503fe34e63deba778fc2da70da8a59

+0

'style.position'の値は何ですか? – chautelly

+0

>>> this.elemento.style.positionが空です –

+0

答えで述べたように、positionは何か他のものに設定し、次に "static"をデフォルトに設定します:https://developer.mozilla.org/en-US/ docs/Web/CSS/top – chautelly

答えて

1

divを絶対位置にしようとしているとします。これを行うには、親コンテナの相対位置を指定する必要があります。 CSSにこれを追加します。あなたはJSでそれを行うことを好む場合

#parent{ 
    position: relative; 
} 
#child{ 
    position: absolute; 
} 

、あなたのJSファイルにこれを追加します。

parent.elemento.style.position = relative; 
this.elemento.style.position = absolute; 
関連する問題