Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

[JQuery] Sortable

More than 1 year has passed since last update.

オブジェクトの順番をイントラクティブに操作させてくえるjQuery UIに含まれるSortableの紹介。 

Sortableのページ
https://api.jqueryui.com/sortable/

APIの詳細
https://api.jqueryui.com/sortable/

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Sortable - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <style>
  #sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
  #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
  #sortable li span { position: absolute; margin-left: -1.3em; }
  </style>
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#sortable" ).sortable();
    $( "#sortable" ).disableSelection();
  } );
  </script>
</head>
<body>

<ul id="sortable">
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 1</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 2</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 3</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 4</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 5</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 6</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 7</li>
</ul>


</body>
</html>

順番を変更したら、新しいポジションを知りたい。そんな時は。

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
    $(function () {
      $("#sortable").sortable({
        start: function (event, ui) {
          ui.item.startPos = ui.item.index();
        },
        stop: function (event, ui) {
          alert("Moved from " + ui.item.startPos + " to " + ui.item.index());
        }
      });
    $("#sortable").disableSelection();
  });
</script>
syantien
トランスコスモス技術研究所にて様々な開発案件を進めています。最近はKotlin頑張ってます。開発メンバー絶賛採用中。このどれかのキーワードでビビっと来た人は面談にお越しください。「ASP.NET C#」, 「Kotlin」,「Swift」,「Angular」,「QA」,「UXデザイナー」,「データサイエンティスト」,「海外開発案件挑戦してみたい」,「Shopify」。
http://syantien.hatenablog.com/
trnd
ソフトウェア・ウェブサービス開発時に読み返せる記事をまとめています。
https://t-rnd.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away