如何通过鼠标进行Div Sortable?

时间:2018-02-23 07:27:55

标签: javascript jquery html

我正在尝试使用鼠标来实现可以排序的div,所选择的项目可以根据用户在列表中重新排序,即它是可拖动的。你能帮帮我..

<html>

<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>
     { 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>

<div style="color:#0000FF; margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px">
    <p>Item 1. </p>
</div>

<div style="color:#0000FF; margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px">
    <p>Item 2. </p>
</div>

<div style="color:#0000FF; margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px">
    <p>Item 3. </p>
</div>

<div style="color:#0000FF; margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px">
    <p>Item 4. </p>
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

您必须将商品换入ID为#sortable

的div中

$(function() {
  $("#sortable").sortable();
  $("#sortable").disableSelection();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div id="sortable">
  <div style="color:#0000FF; margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px">
    <p>Item 1. </p>
  </div>

  <div style="color:#0000FF; margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px">
    <p>Item 2. </p>
  </div>

  <div style="color:#0000FF; margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px">
    <p>Item 3. </p>
  </div>

  <div style="color:#0000FF; margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px">
    <p>Item 4. </p>
  </div>
</div>