使用 jquery.event.drag 拖动多个元素

我想用 jQuery 插件 jquery.event.drag

I want to drag multiple elements with the jQuery plugin jquery.event.drag

这是 原始演示的小提琴:

这里是原始演示的链接:

在演示中,用户点击他想要选择的方块并拖动它们.

On the demo, the user clicks on squares he wants to select and drag them.

但我想做一些最简单的事情:只需点击方块1"并移动所有方块.

But i want to do something simplest : Just click on the square "1" and move all the squares.

我尝试了不同的东西,结果并不好,请看这个小提琴:

I've tried different things and the result is not good, see this fiddle :

http://jsfiddle.net/Vinyl/gVFCL/2/

你能帮我解决这个问题吗?

Can you help me to that that ?

HTML 代码:

<div class="drag" style="left:20px;"></div>
<div class="drag" style="left:100px;"></div>
<div class="drag" style="left:180px;"></div>

CSS 代码

.drag {
    position: absolute;
    border: 1px solid #89B;
    background: #BCE;
    height: 58px;
    width: 58px;
    cursor: move;
    top: 120px;
}
.selected {
    background-color: #ECB;
    border-color: #B98;
    }

jQuery

jQuery(function($){
    $('.drag')
        .click(function(){
            $( this ).toggleClass("selected");
        })
        .drag("init",function(){
            if ( $( this ).is('.selected') )
                return $('.selected');
        })
        .drag(function( ev, dd ){
            $( this ).css({
                top: dd.offsetY,
                left: dd.offsetX
            });
        });
});

编辑Rajagopal 的回答中给出的链接是可以的.我还发现这个插件 MultiDraggable 非常易于使用:https://github.com/someshwara/MultiDraggable

EDIT The link given in Rajagopal's answer is ok. I've also found this plugin MultiDraggable which is really easy to use : https://github.com/someshwara/MultiDraggable

推荐答案

你必须这样做,

$('.drag').drag("init", function(ev, dd) {
    if (this.id == "test") {
        return $(".drag").addClass("selected");
    }
}).drag(function(ev, dd) {
    if (ev.target.id == "test") {
        $(this).css({
            top: dd.offsetY,
            left: dd.offsetX
        });
    }
});​

这是工作 示例.希望这篇文章能帮到你.

Here is the working sample. Hope, this one will help you.

对于这种情况,您可以简单地使用 jquery-ui 可拖动插件.看看这个 http://jqfaq.com/how-to-drag-the-multiple-elements-with-jquery-ui-draggable/.Hoep,这个可以帮助你!

You can simply use jquery-ui draggable plugin for this case. Take a look at this http://jqfaq.com/how-to-drag-the-multiple-elements-with-jquery-ui-draggable/. Hoep, this one will help you!

相关文章