0
我正在使用Jquery UI來實現DIV交換。與拖動和拖放交換不起作用
問題:交換問題已解決,但CSS在交換過程中遇到了一些問題。滾動條在底部出現,div也在交換過程中改變形狀。調換也在尋找這樣不專業:(
誰能告訴我如何使其順利交換
例子是here
<div class="container">
<h1>Drag The Colored DIVs</h1>
<div class="row">
<div class="col-sm-4">
<div class="drophere">
<div class="draghere" style="background-color:yellow;">
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
</div>
</div>
</div>
<div class="col-sm-4">
<div class="drophere">
<div class="draghere" style="background-color:pink;">
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
</div>
</div>
</div>
<div class="col-sm-4">
<div class="drophere">
<div class="draghere" style="background-color:yellow;">
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<div class="drophere">
<div class="draghere" style="background-color:yellow;">
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 44 4 4 4 4 4 4 4 4 4 4 4 4 4 4 44 4 44 44
</div>
</div>
</div>
<div class="col-sm-4">
<div class="drophere">
<div class="draghere" style="background-color:pink;">
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
</div>
</div>
</div>
<div class="col-sm-4">
<div class="drophere">
<div class="draghere" style="background-color:yellow;">
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 66 6
</div>
</div>
</div>
</div>
</div>
CSS:
.draghere {
margin:5px;
}
的Javascript:
$(document).ready(function() {
window.startPos = window.endPos = {};
makeDraggable();
$('.drophere').droppable({
hoverClass: 'hoverClass',
drop: function(event, ui) {
var $from = $(ui.draggable),
$fromParent = $from.parent(),
$to = $(this).children(),
$toParent = $(this);
window.endPos = $to.offset();
swap($from, $from.offset(), window.endPos, 20);
swap($to, window.endPos, window.startPos, 500, function() {
$toParent.html($from.css({
position: 'relative',
left: '',
top: '',
'z-index': ''
}));
$fromParent.html($to.css({
position: 'relative',
left: '',
top: '',
'z-index': ''
}));
makeDraggable();
});
}
});
function makeDraggable() {
$('.draghere').draggable({
zIndex: 99999,
opacity: 0.35,
revert: 'invalid',
start: function(event, ui) {
window.startPos = $(this).offset();
}
});
}
function swap($el, fromPos, toPos, duration, callback) {
$el.css('position', 'absolute')
.css(fromPos)
.animate(toPos, duration, function() {
if (callback) callback();
});
}
});
讓我試試:) – b0y
嘿生長激素卡里姆,我已經試過這種方式,這只是刪除動畫。我想讓CSS動畫:( – b0y
@ b0y沒關係,我沒有提到:-)。請看更新的答案! –