Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

javascript - Is it possible to link two jquery.ui draggables together?

I have two jquery.ui draggables. I am constraining their movement to the y-axis. If one is dragged to a certain y-position, I want the other to automatically move to the same y-position and vice versa. Has anyone ever linked two of these together before?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Updated: Script and demo updated so it is no longer restricted to the y-axis while dragging.

This script looks for the class "group" followed by a number to drag/drop these combined objects. I posted a demo here.

HTML

<div class="demo">
<div id="draggable">
 <p>Drag from here</p>
 <div class="dragme group1"><img src="image1.jpg"><br>Group 1</div>
 <div class="dragme group1"><img src="image2.jpg"><br>Group 1</div>
 <div class="dragme group2"><img src="image3.jpg"><br>Group 2</div>
 <div class="dragme group2"><img src="image4.jpg"><br>Group 2</div>
</div>
<div id="droppable">
 <p>Drop here</p>
</div>
</div>

Script

$(document).ready(function() {
    // function to get matching groups (change '.group' and /group.../ inside the match to whatever class you want to use
    var getAll = function(t) {
        return $('.group' + t.helper.attr('class').match(/group([0-9]+)/)[1]).not(t);
    };
    // add drag functionality
    $(".dragme").draggable({
        revert: true,
        revertDuration: 10,
        // grouped items animate separately, so leave this number low
        containment: '.demo',
        stop: function(e, ui) {
            getAll(ui).css({
                'top': ui.helper.css('top'),
                'left': 0
            });
        },
        drag: function(e, ui) {
            getAll(ui).css({
                'top': ui.helper.css('top'),
                'left': ui.helper.css('left')
            });
        }
    });
    $("#droppable").droppable({
        drop: function(e, ui) {
            ui.draggable.appendTo($(this));
            getAll(ui).appendTo($(this));
        }
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...