我有一个带有预设高度/宽度和CSS样式 overflow: auto
的div中有许多元素(浮动的href标签)。
这是div的结构:
<div id="tagFun_div_main">
<div id="tf_div_tagsReturn">
<!-- all the draggable elements go in here, the parent div scolls -->
</div>
<div id=" tf_div_tagsDrop">
<div id="tf_dropBox"></div>
</div></div>
父级div的'tf_div_tagsReturn'和'tf_div_tagsDrop'最终会相邻浮动。
这是在创建所有'class name'为'tag_cell'的'draggable'元素后运行的jQuery代码:
$(function() {
$(".tag_cell").draggable({
revert: 'invalid',
scroll: false,
containment: '#tagFun_div_main'
});
$("#tf_dropBox").droppable({
accept: '.tag_cell',
hoverClass: 'tf_dropBox_hover',
activeClass: 'tf_dropBox_active',
drop: function(event, ui) {
GLOBAL_ary_tf_tags.push(ui.draggable.html());
tagFun_reload();
}
});
});
正如我之前所述,可拖动的元素只能在div 'tf_div_tagsReturn'内进行拖动,但它们不能在父div范围之外进行可视拖动。值得注意的是,如果我正在拖动其中一个可拖动元素,并将鼠标移动到id为'tf_dropBox'的可放置div上,则会触发hoverclass类,只是我看不到可拖动的元素。
这是我第一次尝试使用jQuery,希望我只是错过了一些非常明显的东西。到目前为止,我一直在阅读文档和搜索论坛,但仍未成功:(
更新:
非常感谢Jabes88提供的解决方案,它使我实现了我正在寻找的功能。这是我的jQuery代码:
$(function() {
$(".tag_cell").draggable({
revert: 'invalid',
scroll: false,
containment: '#tagFun_div_main',
helper: 'clone',
start : function() {
this.style.display="none";
},
stop: function() {
this.style.display="";
}
});
$(".tf_dropBox").droppable({
accept: '.tag_cell',
hoverClass: 'tf_dropBox_hover',
activeClass: 'tf_dropBox_active',
drop: function(event, ui) {
GLOBAL_ary_tf_tags.push(ui.draggable.html());
tagFun_reload();
}
});
});