简体   繁体   English

在拖动一个jqueryUI可拖动元素时,触发对其他多个元素的拖动

[英]On dragging one jqueryUI draggable element trigger drag on other multiple elements

I have for example three draggable elements like follows: 例如,我有三个可拖动元素,如下所示:

<div class="draggable main">Main</div>
<div class="draggable followers">Follower</div>
<div class="draggable followers">Follower2</div>

If .main is dragged, We need .followers to be dragged too. 如果.main被拖动,我们也需要拖动.followers Not just positions but also triggering all the draggable events in them like start, drag, stop. 不仅是位置,而且还会触发其中的所有可拖动事件,例如开始,拖动,停止。 Note, the followers will be normal draggable elements. 请注意,关注者将是普通的可拖动元素。 Just when .main is dragged they should be moving with it as if they are in the same draggable element triggering all the .follower draggable elements' events too. 刚拖动.main时,它们应该随其一起移动,就好像它们处于同一可拖动元素中一样,也触发所有.follower可拖动元素的事件。

Here is the JS 这是JS

jQuery('.draggable.followers').draggable();
jQuery('.draggable.main').draggable({
    // If .main is dragged, We need .followers to be dragged too. Not just positions but also triggering all the draggable events in them like start, drag, stop.
});

Here is the jsfiddle http://jsfiddle.net/9x56E/1/ 这是jsfiddle http://jsfiddle.net/9x56E/1/

Just "cheat" a little by shuffling the HTML around a little like this: 只是通过如下方式将HTML改组一下来“欺骗”一下:

<div class="special-draggable">
    <div class="main">Main</div>
    <div class="draggable followers">Follower</div>
    <div class="draggable followers">Follower2</div>
</div>

Adjust the CSS a little: 稍微调整一下CSS:

.draggable{
    border: 1px solid red;
    width: 60px;
    height: 60px;
}
.main{
    width: 300px;
    border: 1px solid red;
    height: 60px;
}

And then the jQuery: 然后是jQuery:

jQuery('.draggable.followers').draggable();
jQuery('.special-draggable').draggable();

http://jsfiddle.net/Niffler/9x56E/2/ http://jsfiddle.net/Niffler/9x56E/2/



EDIT : Sorry, missed the part about it having to trigger the draggable events... 编辑 :抱歉,错过了有关必须触发可拖动事件的部分...

How about this: 这个怎么样:

$(function() {
    $('.draggable.followers').draggable();
    $('.draggable.main').draggable();

    /* trigger dragstart on followers when main is dragged */
    $('.draggable.main').on('dragstart', function() {
        $('.draggable.followers').each(function() {
            $(this).trigger('dragstart');
        });
    });

    /* trigger drag on followers when main is dragged and adjust position */
    $('.draggable.main').on('drag', function() {
        var maintop = $(this).css('top');
        var mainleft = $(this).css('left');

        $('.draggable.followers').each(function() {
            $(this).trigger('drag');
            $(this).addClass('ui-draggable-dragging');
            $(this).css('margin-left', mainleft);
        });

        $('.draggable.followers:first').css('margin-top', maintop);
    });

    /* trigger dragstop on followers when main is dragged and adjust position */
    $('.draggable.main').on('dragstop', function() {
        var maintop = $(this).css('top');
        var mainleft = $(this).css('left');

        $('.draggable.followers').each(function() {
            $(this).trigger('dragstop');
            $(this).removeClass('ui-draggable-dragging');
            $(this).css('margin-left', mainleft);
        });

       $('.draggable.followers:first').css('margin-top', maintop);
    });


    /* test different actions on .followers */
    $('.draggable.followers').on('dragstart', function() {
        // do something
    });

    $('.draggable.followers').on('drag', function() {
        // do something
    });

    $('.draggable.followers').on('dragstop', function() {
        // do something
    });

});

Here's a fiddle: http://jsfiddle.net/Niffler/6TZp3/ 这是一个小提琴: http : //jsfiddle.net/Niffler/6TZp3/

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

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