【发布时间】:2012-12-27 13:41:27
【问题描述】:
我想用jQuery插件jquery.event.drag拖动多个元素
这是fiddle of the original demo:
这是original demo的链接:
在演示中,用户点击他想要选择的方块并拖动它们。
但我想做一些最简单的事情:只需点击方块“1”并移动所有方块。
我尝试了不同的东西,结果并不好,请看这个小提琴:
http://jsfiddle.net/Vinyl/gVFCL/2/
你能帮我解决这个问题吗?
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
【问题讨论】:
标签: javascript jquery jquery-ui jquery-plugins drag-and-drop