简体   繁体   English

Parent and Child DIV's not nested position 更改 jquery javascript 问题

[英]Parent and Child DIV's not nested position change jquery javascript question

If I have 2 divs, DIV A and div b (assume both are absolute positioned).. I want to amke it so that when DIV A's position changes, DIV B (which is the "child" of div A, event though they are two separate divs, not nested), div B's position changes with the same offsets that div A did.如果我有 2 个 div,DIV A 和 div b(假设两者都是绝对定位的).. 我想修改它,以便当 DIV A 的 position 发生变化时,DIV B(这是 div A 的“孩子”,尽管它们是两个单独的 div,未嵌套),div B 的 position 变化与 div A 所做的相同偏移量。 However if I move DIV B's position... then just DIV B moveS and DIV A's position is unaffected.但是,如果我移动 DIV B 的 position... 那么只有 DIV B 移动,而 DIV A 的 position 不受影响。

Change in positon would be by dragging the div for example.例如,可以通过拖动 div 来改变位置。

If possible, I would like to be able to make it so that I can have a 3rd div, and say its a "child" of div A or B, and the same action would apply.如果可能的话,我希望能够做到这一点,以便我可以拥有第三个 div,并说它是 div A 或 B 的“孩子”,并且将适用相同的操作。 (Ex1. if div C and B are children of div A, then moving div A would update position of DIV C and B) (例如 1。如果 div C 和 B 是 div A 的子级,则移动 div A 将更新 DIV C 和 B 的 position)

Ex2.例 2。 If div C is child of DIV B, then WHen moving DIVA, DIV B and C would be updated.如果 div C 是 DIV B 的子级,则在移动 DIVA 时,DIV B 和 C 将被更新。 If just DIVB was moved, then DIV B and C would be updated.如果只移动了 DIVB,则 DIV B 和 C 将被更新。 If moving DIV C, then only DIV C would be updated.如果移动 DIV C,则只会更新 DIV C。

Update: Hussein's code is similar to what I want, but the problem is the div of class.b happens to always snap back to where it originally was as opposed to preserving its current position and adding the offset as.a moves.更新:侯赛因的代码与我想要的类似,但问题是 class.b 的 div 碰巧总是突然回到原来的位置,而不是保留其当前的 position 并添加偏移量。

Assuming you are using jQuery ui draggable() , use the drag event to achieve this.假设您使用的是 jQuery ui draggable() ,请使用拖动事件来实现这一点。 You may need to modify this code further to suit your needs, but the example below should get you on the right track.您可能需要进一步修改此代码以满足您的需要,但下面的示例应该让您走上正轨。

$('.a, .b').draggable({
    drag: function(event, ui) {
        $('.a').css({
            top: ui.offset.top + 'px',
            left: ui.offset.left + 100 + 'px'
        })
    }
});

Check working example at http://jsfiddle.net/7tDjc/http://jsfiddle.net/7tDjc/检查工作示例

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

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