2017-08-16 15 views
0

JQuery UIでは、ドラッグ可能な要素をコンテナ内の特定の要素(.container)に制限しようとしています。ドラッグ可能な要素を特定の要素に限定する方法

でも値の配列としてcontainmentを試してみましたが、私の場合は、.containerの高さと幅を認識しません。これを行うには適切なアプローチを教えてください。

<div class="container"> 
    <div class="restricted-field-1">should be restricted here</div> 
    <div class="dragme">DRAG ME</div> 
    <div class="restricted-field-2">should be restricted here</div> 
</div> 

$(".dragme").draggable({ 
    containment: ".container" 
}); 

JSFIDDLE

+0

白い背景領域内のドラッグを制限しようとしていますか? – kmg

+0

この投稿は役に立ちました:https://stackoverflow.com/questions/11452185/restrict-jquery-draggable-items-from-overlapping-colliding-with-sibling-elements –

+0

この投稿は参考になるかもしれません:https:// stackoverflow .com/questions/11452185/restrict-jquery-draggable-items-overlapping-colliding-with-sibling-elements –

答えて

0

$(".dragme").draggable({ 
 
\t containment: ".mini" 
 
});
.container{ 
 
    position:relative; 
 
    width: 500px; 
 
    height: 400px; 
 
    background: #FFF; 
 
} 
 

 
.dragme{ 
 
    width: 100px; 
 
    cursor: move; 
 
    background: black; 
 
    color:white; 
 
    text-align:center; 
 
} 
 
.restricted-field-1{ 
 
    width:480px; 
 
    background: silver; 
 
    padding:10px; 
 
    user-select:none; 
 
    height: 20px; 
 
} 
 
.restricted-field-2{ 
 
    position:absolute; 
 
    width:480px; 
 
    bottom:0; 
 
    padding:10px; 
 
    background: silver; 
 
    user-select:none; 
 
    height:20px; 
 

 
    
 
} 
 
.mini{ 
 
    position: absolute; 
 
    top: 40px; 
 
    left: 0px; 
 
    bottom: 40px; 
 
    
 
    width: 100%; 
 

 
    
 
}
<div class="container"> 
 
    <div class="restricted-field-1">should be restricted here</div> 
 
    <div class="mini"> 
 
    <div class="dragme">DRAG ME</div> 
 
    </div> 
 
    
 
    <div class="restricted-field-2">should be restricted here</div> 
 
</div> 
 

 
<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>

+0

ありがとう実際には、コンテナ内に新しい要素を作成する必要はありません。 – Kumar

1

あなたは、.dragmeをラップする.containerのdivを移動.containerposition: relativeを削除して、スタイルの変更を次のように設定することができます。

次のように変更します。

.container { 
    position: absolute; 
    height: 362px; 
} 
.restricted-field-2 { 
    top: 400px; 
} 

Hereはjsfiddleリンクです。

編集:

が封じ込めのためのx-axisy-axis位置を設定するにはドラッグ可能なjqueryの内のオプションがあり、私たちは私たちの要求に基づいて計算する必要があります。

Hereは更新されたjsのフィドルリンクです。

+0

はい、これは動作しますが、実際にはjquery UI側(スクリプト側)で変更を行う必要があります。ありがとうございます – Kumar

+0

私はあなたが言及したようにスクリプト側の変更だけで更新されたjsのフィドルリンクを追加しました。希望が役立ちます。 – kmg

+0

はい、これは動作しますが、ここではコンテナと制限された要素の正確な高さと幅はわかりません。 ありがとう – Kumar

関連する問題