开发者

Positioning an element using values from Javascript variables

How to position an element for e.g. a div tag by using values for x & y co-ordinate开发者_JS百科s from Javascript variables ?


If you're trying to position the object in absolute x,y coordinates on the page, just set the object to position: absolute and then set the values of top and left to your x and y.

HTML:

<div id="square" style="background-color: #777; height: 100px; width: 100px;"></div>

Code:

var s = document.getElementById("square");
s.style.position = "absolute";
var x = 100, y = 200;
s.style.left = x + "px";
s.style.top = y + "px";

You can see a working code example here: http://jsfiddle.net/jfriend00/hhpDQ/.


I'm not sure you can set the coordinates, but you could set the margin. Something like

$("#divid").css("margin-left" : "200px" , "margin-up" : "200px");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜