grouping draggable objects with jquery-ui draggable

前端 未结 4 671
青春惊慌失措
青春惊慌失措 2020-11-28 20:20

I want to use jquery draggable/droppable to let the user select a group of objects (each one has a checkbox in the corner) and then drag all the selected objects as a group.

4条回答
  •  囚心锁ツ
    2020-11-28 21:05

    What I've done for this is created a function that you give the slave / master elements to, which creates a bind() function for the master (I'm only allowing a drag from the master in this case, you can work around this I'm sure), which makes the slave follow it around using standard jQuery css.

        function overlap(slave,master) {
            $('a#groupTheseBlocks').click(function(){
                master.bind('drag', groupBlocks);
                slave.draggable('disable');
    
                // remember where the slave is in relation to the master
                sLeftRef = (slave.offset().left - master.offset().left);
                sTopRef = (slave.offset().top - master.offset().top);
            });
    
    
            function groupBlocks() {
                var left = master.offset().left;
                var top = master.offset().top;
    
                slave.draggable('disable');
                slave.css('left', (left + sLeftRef) + 'px');
                slave.css('top', (top + sTopRef) + 'px');
    
            } 
        }
    

    I guess I'll post more to this once I have a working example. As it stands this is working for me. What is missing is a way to call overlap(slave, master) with the elements you want to group together. I'm doing this in a really specific way. You can clever a way to do it, I'm sure.

提交回复
热议问题