問題
解決策
jQuery UIのSortableのオプションで「handle」で要素を指定することで、その要素をドラッグの領域にすることができます。
スマートフォン時のドラッグ操作の対策
jQuery UIのSortableをそのまま設定すると、領域が丸々ドラッグできてしまうため、スマートフォン時にドラッグしたくないのにドラッグさせてしまうことになります。
それを防ぐために、今回はjQuery UIのSortableでドラッグの出来る領域を設定する方法をご紹介します。
DEMOはこちら
項目 | 内容 | |
---|---|---|
項目01 | 内容01 | |
項目02 | 内容02 | |
項目03 | 内容03 | |
項目04 | 内容04 | |
項目05 | 内容05 |
CDNを利用して、jQuery UIを読み込みます。
CDNを利用して、jQuery UIを読み込みます。
<head>
...
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js">
...
</head>
HTML
ドラッグの領域用として、クラス「dragicon」を用意します。
<div class="drag-box">
<table class="type03 dragtable">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">項目</th>
<th scope="col">内容</th>
</tr>
</thead>
<tbody>
<tr id="num01">
<th scope="row"><span class="dragicon"></span></th>
<td>項目01</td>
<td>内容01</td>
</tr>
<tr id="num02">
<th scope="row"><span class="dragicon"></span></th>
<td>項目02</td>
<td>内容02</td>
</tr>
<tr id="num03">
<th scope="row"><span class="dragicon"></span></th>
<td>項目03</td>
<td>内容03</td>
</tr>
<tr id="num04">
<th scope="row"><span class="dragicon"></span></th>
<td>項目04</td>
<td>内容04</td>
</tr>
<tr id="num05">
<th scope="row"><span class="dragicon"></span></th>
<td>項目05</td>
<td>内容05</td>
</tr>
</tbody>
</table>
</div>
CSS
ドラッグの領域用であるクラス「dragicon」とドラッグ時のcssを設定します。
.dragicon{
display: inline-block;
width: 30px;
height: 30px;
background-color: #ffffff;
background-image: url("../images/common/anchor-drag.png");
background-position: center center;
background-repeat: no-repeat;
background-size: 14px auto;
border: 1px solid #F15A24;
border-radius: 50%;
cursor: move;
}
.dragicon:hover{
background-color: #fef2ef;
}
.drag{
background-color: #f9f9f9;
border: dotted 2px #F15A24;
}
javascript
jQuery UIのSortableのオプションで「handle」で要素を指定することで、その要素をドラッグの領域にすることができます。
$('.dragtable tbody').sortable({
opacity: 0.5,
placeholder: "drag",
handle: '.dragicon',
axis: 'y'
});
参照
【ドラッグ&ドロップで並べ替えるときのドラッグできる部分を指定】
http://alphasis.info/2011/06/jquery-ui-sortable-handle/
コメント