简体   繁体   English

如何根据另一个“ div”元素水平对齐一个“ div”元素

[英]How to align a 'div' element horizontally according to another 'div' element

I have two 'div' elements.The first one changes its position dymanically when the window(browser's window) is resized(including dragging the borders of the window). 我有两个'div'元素。第一个在调整窗口(浏览器窗口)大小(包括拖动窗口的边框)时动态地​​改变其位置。 The other one is static and doesn't move.I can not put them in the same div because they are in two different files that get loaded in the same page as partial views. 另一个是静态的,不会移动。我不能将它们放在同一个div中,因为它们位于两个不同的文件中,这些文件作为局部视图加载在同一页中。

I know that you probably want to see the divs themselves and the styles applied to them but I don't kno if I won't get it even more messed up for explaining. 我知道您可能希望看到div本身以及应用于它们的样式,但是我也不知道是否可以弄得更糟以进行解释。

I was just wondering if it is possible by JavaScript or whatever to get the position(and more specially the horizontal positioning) of an existing 'div' element and assign it to another so whenever I resize the window the second 'div' gets situated according to the other one? 我只是想知道是否可以通过JavaScript或其他方法获取现有'div'元素的位置(尤其是水平位置)并将其分配给另一个元素,因此每当我调整窗口大小时,第二个'div'就会根据给另一个?

EDIT : I am pretty much sure that I have to change the 'offsetLeft' property of the second 'div', because when I resize the window this is what dynamically changes in the first one's properties. 编辑:我非常确定我必须更改第二个“ div”的“ offsetLeft”属性,因为当我调整窗口大小时,这是第一个属性的动态变化。

Use jQuery and you can retrieve all kinds of information about the positioning of elements, quite easily. 使用jQuery,您可以很容易地检索有关元素位置的各种信息。

A great piece of jquery that will do this, is the $.position() function: $.position()函数是完成此任务的一个很棒的jquery:

http://api.jquery.com/position/ http://api.jquery.com/position/

Fiddle: 小提琴:

http://jsfiddle.net/cQ4aX/1/ http://jsfiddle.net/cQ4aX/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM