- 页面将具有两个div:一个是#origin,包含许多缩略图图像,第二个是#drop,其中可以放置图像(最多只能放3张)。
- 需要可以从#drop拖放图像回到原始位置。
- #drop需要可排序,因为所选3个图像的顺序很重要。
- 将图像放在#drop上时,这些图像应该排列,就像它们从一开始就是这样显示的。
我以前从未使用过jQuery,甚至在之前的工作页面原型中,我已经通过本地HTML5拖放事件在两个容器之间实现了拖放,但是当我将sortable() jQuery功能添加到#drop时,这些图像不能再被拖回到#origin,这破坏了我的功能。
所以,我重新开始,并想要使用jQuery实现拖放功能和排序功能。我已经阅读了所有可拖动、可放置和可排序功能的API,但我在实现此功能时遇到了麻烦。不确定是每个功能使用的设置是否正确。
在小提琴上,您可以看到图像变得可拖动,我可以看到我指定的设置生效(不透明度、光标),但是这些图像无法放置在#drop上。据我所知,使图像可拖动,图像具有“draggable”类,并使用“.draggable”接受使#drop可放置的组合应该允许最基本的功能,但即使如此,也似乎无法实现。另外,我读到如果可拖动和可放置都具有相同的“scope”设置,它也应该起作用,但事实并非如此。
以下是页面代码的简单版本,以及jsFiddle链接:http://jsfiddle.net/39khs/ 代码:
CSS:
#origin {
background-color: green;
}
#drop {
background-color: red;
min-height: 120px;
}
js:
$("img").draggable({ helper: "clone", opacity: 0.5, cursor: "crosshair", scope: "drop" });
$("#drop").droppable({ accept: ".draggable", scope: "drop"});
$("#drop").sortable();
html:
<div id="wrapper">
<div id="origin" class="fbox">
<img src="http://placehold.it/140x100" id="one" title="one" class="draggable" />
<img src="http://placehold.it/140x100" id="two" title="two" class="draggable" />
<img src="http://placehold.it/140x100" id="three" title="three" class="draggable" />
</div>
<p>test</p>
<div id="drop" class="fbox">
</div>
</div>
任何帮助都非常感谢。