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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14055639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:35:48  来源:igfitidea点击:

Drag multiple elements with jquery.event.drag

javascriptjqueryjquery-uijquery-pluginsdrag-and-drop

提问by Sébastien Gicquel

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

我想使用 jQuery 插件jquery.event.drag拖动多个元素

Here is a fiddle of the original demo:

这是原始演示小提琴

Here is the link to the original demo:

这是原始演示的链接:

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

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

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

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

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

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

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

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

Can you help me to that that ?

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

HTML code :

HTML代码:

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

CSS code

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

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
            });
        });
});

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

编辑 Rajagopal 的回答中给出的链接没问题。我还发现这个插件 MultiDraggable 非常好用:https: //github.com/someshwara/MultiDraggable

采纳答案by Rajagopal ?

You have to do something like this,

你必须做这样的事情,

$('.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.

这是工作示例。希望,这个会帮助你。

EDIT:

编辑:

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!

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

回答by Jose Vega

jQuery(function($) {

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

Demo http://jsfiddle.net/gVFCL/3/

演示http://jsfiddle.net/gVFCL/3/

回答by Rafaelox

My code upgrade multi drag and multi select:

我的代码升级多拖多选:

http://jsfiddle.net/F4AwY/

http://jsfiddle.net/F4AwY/

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

$('div[class*="drag"]').click(function(){
    $(this).toggleClass("selected");
})

回答by Chaitanya Munipalle

In the original jsfiddle change

在原来的 jsfiddle 改变

.click(function(){
        $(this).toggleClass("selected");
    })

to

.click(function(){
        $('.drag').toggleClass("selected");
    })