由于您使用的是 jQuery UI,您可以利用 droppable 小部件。这有两个回调,over 和 out。每当可拖动元素超出或超出可放置区域时,更改其 CSS 属性。
看到它工作here。
$(".columns_all").droppable({
over: function (event, ui) {
ui.helper.css("background-color", "lightblue");
},
out: function (event, ui) {
ui.helper.css("background-color", "");
}
});
更新
根据要求,这里是双向拖动的代码。
elwidth = $(".columns_selected li").width(); // used to set element width
// left to right
$(".columns_all").droppable({
over: function (event, ui) {
ui.helper.css({
"background-color": "lightblue",
width: "auto"
});
},
out: function (event, ui) {
ui.helper.css({
"background-color": "",
width: elwidth
});
},
drop: function (event, ui) {
ui.helper.css({
"background-color": "#ccc"
});
}
});
// right to left
$(".columns_selected").droppable({
over: function (event, ui) {
ui.helper.css({
"background-color": "red",
width: elwidth
});
},
out: function (event, ui) {
ui.helper.css({
"background-color": "",
width: "auto"
});
},
drop: function (event, ui) {
ui.helper.css({
"background-color": "#74ce9c"
});
}
});
更新fiddle。