简体   繁体   English

将绝对定位转换为相对定位

[英]Convert Absolute Positioning Into Relative Positioning

Sorry if this is already out there somewhere, but I've been looking for a while and haven't found anything. 抱歉,如果已经在某个地方了,但是我已经找了一段时间,还没有找到任何东西。 I'd like to know if there's any way to, given the x and y coordinates/offset for an absolutely positioned element on a page, change that element into a relatively positioned element, but have it stay in the same visual spot, changing its location in the DOM. 我想知道是否可以通过给定页面上绝对定位元素的x和y坐标/偏移量,将该元素更改为相对定位的元素,但将其保留在同一视觉位置,从而更改其位置DOM中的位置。 For example, given this html 例如,给定此html

<div id="divOne"></div>
<div id="divTwo"></div>
<div id="divThree"></div>

if divOne were positioned absolutely and its position happened to visually fall between divTwo and divThree, is there a way for me to convert its x,y position so I would be able to tell jQuery to place it after divOne and before divTwo in the DOM? 如果divOne绝对定位并且其位置恰好在视觉上介于divTwo和divThree之间,是否有办法让我转换其x,y位置,以便我可以告诉jQuery将其放在DOM中的divOne之后和divTwo之前? I'm well versed in Javascript and jQuery, I'm just looking for a method I may not know about or for an answer from someone who may have come across this before. 我精通Javascript和jQuery,我只是在寻找一种我可能不知道的方法,或者寻找以前可能遇到过的人的答案。

by removing the absolute positioning of the dragged div you must place its tag to a different place in the dom in order to get it repositioned. 通过删除拖动的div的绝对位置,必须将其标签放置在dom中的其他位置,以便重新放置它。

For example if you drag #divOne so that it visually moves between #divTwo and #divThree what you should do is remove the absolute positioning of #divOne and move its tag between the two other divs: 例如,如果拖动#divOne以便它在#divTwo和#divThree之间可视地移动,则应该删除#divOne的绝对位置,并将其标签移动到其他两个div之间:

<div id="divTwo"></div>
<div id="divOne"></div>
<div id="divThree"></div>

If you have a well-defined grid this will work. 如果您有一个定义明确的网格,那么它将起作用。

If I were you what I would do is to give a standard class to any div in my grid that I want be re-arrangeable. 如果我是你,我会做的是给我希望重新安排的网格中的任何div一个标准类。 Eg, the class "arrangeable". 例如,类“可安排”。 When the drag-end fires I would calculate where my dragged div must be placed in the dom using this formula: 当后端被触发时,我将使用以下公式计算将div放置在dom中的位置:

The dragged div should be moved to another place on the dom. 拖动的div应该移到dom的另一个位置。 What it will happen is that the dragged div will take the place of an existing one and "push" the existing just after it. 将会发生的事情是,拖动的div将取代现有的div,并在其之后“推动”现有的div。 For example by dragging #divOne between #divTwo and #divThree, #divOne takes the place of #divThree and pushes it after it. 例如,通过将#divOne拖动到#divTwo和#divThree之间,#divOne将代替#divThree并将其推入到它之后。 Supposing that the user stops the drag and releases left click when the dragged div is over the existing div whose place is going to be taken (let's name it "pushedDiv") by the dragged one then all you have to do is to recognize the pushedDiv, remove the dragged one from the dom, place right before the recognized one and make its position relative. 假设用户停止了拖动并在被拖动的div超过要被其拖动的位置取代现有div的现有div时释放鼠标左键(我们将其命名为“ pushedDiv”),那么您要做的就是识别pushDiv ,从dom中删除拖动的一个,将其放置在已识别的dom之前,并使其位置相对。

In order to realize which is the pushedDiv you can use this routine: 为了了解哪个是pushDiv,可以使用以下例程:

//on drag end, where mouse has x and y coords:
var pushedDiv;
$(".arrangeable").each(function(index,value){
    var theoffset = $(this).offset();
    if(x >= theoffset .left && x <= theoffset .left + $(this).width() && y >= theoffset .top && y <= theoffset .top + $(this).height()){
         pushedDiv = $(this);
    }
});
// if pushedDiv is still null then the user didn't release the left click over a div of class "arrangeable". Else, the pushedDiv will be the one we are looking for

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

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