1 つには、jQuery プラグインと独自のコードの両方を介して画像をドラッグ可能にしました。あなたのコードは の を変更してbackground-position
おりdiv
、jQuery プラグインはdiv
の実際の位置を変更しています。それはいくつかの問題を引き起こすに違いありません。
また、Draggableのcontainment
パラメーターは、あなたがやろうとしているような大きなものではなく、親コンテナーよりも小さいドラッグ可能なアイテム用に設計されているようです。
とにかく、ここに作業コードがあります:
$(document).ready(function() {
$(".draggable").draggable().bind('dragstop', function(e, ui) {
if (ui.position.top > 0) {
$(this).css('top', 0);
}
if (ui.position.left > 0) {
$(this).css('left', 0);
}
var bottom = -($(this).height() - $(this).parent().height()),
right = -($(this).width() - $(this).parent().width());
if (ui.position.top < bottom) {
$(this).css('top', bottom);
}
if (ui.position.left < right) {
$(this).css('left', right);
}
});
});
エッジ スナップが必要ない場合は、.bind()
関数を削除して、 を呼び出すだけ.draggable()
です。
$(document).ready(function() {
$(".draggable").draggable();
});