Ann*_*nie 1 javascript jquery html5 drag-and-drop jquery-ui
我正在关注jQuery draggable项目的这个指南.
我唯一想念的是dragenter和dragleave临时元素的CSS一起玩的事件.它只hoverClass为可拖动元素提供属性,当可拖动元素移动到它们上时.如果我们需要更改其他元素的CSS怎么样?
当前代码如下所示:
$(".part").draggable({
start: startDrag,
stop: stopDrag,
revert: true
});
$(".parts-action").droppable({
accept: '.part',
hoverClass: 'hovered',
drop: handleDrop
});
Run Code Online (Sandbox Code Playgroud)
除此之外,我还需要:
$(".wrapper").on('dragenter', function (e) {
$(this).next(".parts-action").show();
});
$(".wrapper").on('dragleave', function (e) {
$(this).next(".parts-action").hide();
});
Run Code Online (Sandbox Code Playgroud)
但它在使用时不起作用draggable().
我也尝试过:
$(".part").draggable({
drag: handleDrag
})
function handleDrag(e, ui){
/* how to know if active draggable is on some element other than droppable? */
}
Run Code Online (Sandbox Code Playgroud)
这是非常令人困惑的,因为它与可拖动和可拖放相关联!
我不是100%确定我完全理解你的问题以及你需要做什么,但如果你需要更改其他元素的CSS,那么你可以使用over和out事件处理程序.看一下Droppable的jQueryUI文档.您基本上想要在over事件触发时添加一个类,并在out事件触发时删除该类.
HTML
<div class="ui-widget-content part">
<p>Drag me to my target</p>
</div>
<div class="ui-widget-header parts-action">
<p>Droppable area 1</p>
</div>
<div class="ui-widget-header parts-action">
<p>Droppable area 2</p>
</div>
<div class="ui-widget-header parts-action">
<p>Droppable area 3</p>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS
.parts-action { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
.part { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
.parts-action-highlight { background: red; }
Run Code Online (Sandbox Code Playgroud)
jQuery的
$(function() {
$( ".part" ).draggable();
$( ".parts-action" ).droppable({
accept: ".part",
hoverClass: "ui-state-active",
drop: function( event, ui ) {
$(this)
.addClass( "ui-state-highlight" )
.find( "p" )
.html( "Dropped!" );
},
over: function( event, ui ) {
$(this)
.siblings(".parts-action")
.addClass("parts-action-highlight");
},
out: function( event, ui ) {
$(this)
.siblings(".parts-action")
.removeClass("parts-action-highlight");
}
});
});
Run Code Online (Sandbox Code Playgroud)
上面的大部分代码示例都来自jQueryUI droppabe示例,因此ui-widget-XXX类不需要/与您相关,只是留下来突出显示效果.
只需用您需要采取的任何操作替换函数调用的主体,例如
over: function( event, ui ) {
$(this)
.siblings(".parts-action")
.hide();
},
out: function( event, ui ) {
$(this)
.siblings(".parts-action")
.show();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1848 次 |
| 最近记录: |