简体   繁体   English

JQuery - offset()/ position()的回调

[英]JQuery - Callback for offset()/position()

I have a div containing a list elements, which have static positioning and for each element I want to 我有一个包含列表元素的div ,它具有静态定位和我想要的每个元素

  1. Record its absolute positioning using the offset() method 使用offset()方法记录其绝对定位
  2. Change its positioning to absolute , move it somewhere else, and later move it back to its original position using the recorded coordinates 将其定位更改为absolute ,将其移动到其他位置,然后使用记录的坐标将其移回原始位置

First I tried this: 首先我尝试了这个:

var positions = {};
myDiv.children().each(function(){
    var child = $(this);
    var id = child.attr('id');
    var offset = child.offset();
    positions[id] = [offset.left, offset.top];

    child .css('position', 'absolute');
    var x = ...;
    var y = ...;
    child.css('left', x);
    child.css('top', y);
});

console.log(positions);

In this case, every entry of the 'positions' object have the same coordinates (value for the first element). 在这种情况下,“位置”对象的每个条目具有相同的坐标(第一个元素的值)。 However, if I comment out the bit that moves the element, the positions objects has the correct coordinates for each child. 但是,如果我注释掉移动元素的位,则positions对象具有每个子元素的正确坐标。

So I'm guessing it's a synchronisation issue, because the css function is called before the offset function has terminated. 所以我猜这是一个同步问题,因为在offset函数终止之前调用了css函数。

So what I need really, is to make sure that the block child.css('left', x); child.css('top', y); 所以我真正需要的是确保块child.css('left', x); child.css('top', y); child.css('left', x); child.css('top', y); is only executed when the 'offset' function is done. 仅在'offset'功能完成时执行。

I tried things like 我尝试过类似的东西

$.queue(child, 'foo', function() {
        var offset = $(this).offset();
        positions[id] = [offset.left, offset.top];
        jQuery.dequeue(this);
    });
$.dequeue(child, 'foo');
child.promise().done(function() {
        child.css('position', 'absolute');
        ...
    });

but with no luck: when I log the positions objects it still has the wrong coordinates. 但没有运气:当我记录positions对象时,它仍然有错误的坐标。

I am quite new to JS and JQuery so I'm sure how to do the right thing here. 我是JS和JQuery的新手,所以我很确定如何在这里做正确的事情。

How can I make sure the block child.css('left', x); child.css('top', y); 我怎样才能确定块child.css('left', x); child.css('top', y); child.css('left', x); child.css('top', y); will only be executed after the 'positions' object has been populated with the correct offset coordinates? 只有在'position'对象填充了正确的偏移坐标后才会执行?

EDIT I just realised that in my case I need to use 'position()' rather than 'offset()' but that doesn't really change the problem that I have described 编辑我刚刚意识到在我的情况下我需要使用'position()'而不是'offset()'但这并没有真正改变我所描述的问题

In .each() changing sibling elements' position could be cause of getting wrong coordinates after the first one. .each()改变兄弟元素的位置可能导致在第一个之后得到错误的坐标。

Why don't you get and store all positions before changing? 为什么不在变更之前获得并存储所有职位? I mean you should iterate childrens double times. 我的意思是你应该重复儿童两次。

  1. Iterate childrens and store position, don't change any css, 迭代孩子和商店的位置,不要改变任何CSS,
  2. Iterate childrens again and change css. 再次迭代孩子并改变css。

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

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