【问题标题】:When clicking and dragging sortable jQuery div, the item jumps from middle of browser to the left单击并拖动可排序的jQuery div时,项目从浏览器中间跳到左侧
【发布时间】:2019-02-20 15:40:50
【问题描述】:

对可排序列表中的项目进行排序时,项目会在排序/拖动时从浏览器中的中心位置跳到最左侧。

点击两次添加任务,然后将一个项目排序到新位置,你就会明白我在说什么了。

$(document).ready(function() {
  $(function() {
    $('#sortable').sortable();
    $('#sortable').disableSelection();

  });

  $(document).ready(function() {
    $('#add').on('click', () => {
      $('.ul').append(
        '<div class="divvy">' +
        '<input type="text" class="inputty"/><button class="remove" id="deletestyle" style="float: right;"> X </button>' +
        '<div class="detailcontainer" style="float: left;" > <p>▼</p></div><div class="panel">' +
        '<form class="form-inline"><p>Details</p><br><textarea name="details" rows="6" cols="15">' +
        '</textarea><p>Due Date</p><input type="date" name="date" style="margin-bottom: 25px; width: 127px;"></form></div></div>');
    });

    $('.ul').on('click', '.detailcontainer', function() {
      $(this).closest('.divvy').find('.panel').toggle();
    });
  });

  $('.panel').hide();
  $('.optionBox').on('click', '.remove', function() {
    $(this).parent().fadeOut(400, function() {
      $(this).remove();
    });
  });
});
.panel {
  display: none;
}

.center {
  text-align: center;
  margin-top: 58px;
}

.center div {
  margin: 0 auto;
}

.form-inline {
  display: flex;
  flex-flow: row wrap;
  align-items: center;
}

#deletestyle {
  background: #f04d25;
  border: solid 1px white;
  color: white;
  font-weight: 700;
  height: 45px;
  width: 10%;
  border-radius: 0px;
}

.divvy {
  border: solid 1px black;
  padding: 10px;
  width: 35%;
  border-radius: 2px;
  background: #C0C0C0;
  position: relative;
  min-width: 325px;
  margin-left: auto;
  margin-right: auto;
  overflow: auto;
}

.divvy:hover {
  border: solid 2px darkgray;
  padding: 10px;
  width: 35%;
  border-radius: 2px;
  background: #C0C0C0;
  min-width: 325px;
  margin: 0 auto;
}

.divvy:active {
  border: solid 2px darkgray;
  padding: 10px;
  width: 35%;
  border-radius: 2px;
  background: #C0C0C0;
  -webkit-box-shadow: 2px 2px 8px 2px rgba(0, 0, 0, 0.15);
  -moz-box-shadow: 2px 2px 8px 2px rgba(0, 0, 0, 0.15);
  box-shadow: 2px 2px 8px 2px rgba(0, 0, 0, 0.15);
  min-width: 325px;
  margin: 0 auto;
}

.inputty {
  width: 75%;
  height: 45px;
  font-size: 22px;
  font-family: 'work sans';
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Work+Sans" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<div class="center">
  <div class="optionBox" style="position: relative;">
    <button class="addtask" id="add" class="center">+  ADD TASK</button>
    <div id="sortable" class="ul" class="center"></div>
  </div>
</div>

(fiddle)

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    #sortable 指定宽度似乎可以解决问题:

    #sortable {
      width: 380px;
    }
    

    【讨论】:

      【解决方案2】:

      您可以简单地添加到您的 .divvy 类:

      .divvy {
        top:0;
        right:0;
        left:0;
      }
      

      $(document).ready(function() {
      
        $(function() {
          $("#sortable").sortable();
          $("#sortable").disableSelection();
        });
      
        $(document).ready(function() {
          $('#add').on('click', () => {
            $('.ul').append(
              '<div class="divvy">' +
              '<input type="text" class="inputty"/><button class="remove" id="deletestyle" style="float: right;"> X </button>' +
              '<div class="detailcontainer" style="float: left;" > <p>▼</p></div><div class="panel">' +
              '<form class="form-inline"><p>Details</p><br><textarea name="details" rows="6" cols="15">' +
              '</textarea><p>Due Date</p><input type="date" name="date" style="margin-bottom: 25px; width: 127px;"></form></div></div>');
          });
      
          $('.ul').on('click', '.detailcontainer', function() {
            $(this).closest('.divvy').find('.panel').toggle();
          });
        });
      
        $('.panel').hide();
      
        $('.optionBox').on('click', '.remove', function() {
          $(this).parent().fadeOut(400, function() {
            $(this).remove();
          });
        });
      });
      .panel {
        display: none;
      }
      
      .center {
        text-align: center;
        margin-top: 58px;
      }
      
      .center div {
        margin: 0 auto;
      }
      
      .form-inline {
        display: flex;
        flex-flow: row wrap;
        align-items: center;
      }
      
      #deletestyle {
        background: #f04d25;
        border: solid 1px white;
        color: white;
        font-weight: 700;
        height: 45px;
        width: 10%;
        border-radius: 0px;
      }
      
      .divvy {
        border: solid 1px black;
        padding: 10px;
        width: 35%;
        border-radius: 2px;
        background: #C0C0C0;
        position: relative;
        min-width: 325px;
        margin-left: auto;
        margin-right: auto;
        overflow: auto;
        top: 0;
        right: 0;
        left: 0;
      }
      
      .divvy:hover {
        border: solid 2px darkgray;
        padding: 10px;
        width: 35%;
        border-radius: 2px;
        background: #C0C0C0;
        min-width: 325px;
        margin: 0 auto;
      }
      
      .divvy:active {
        border: solid 2px darkgray;
        padding: 10px;
        width: 35%;
        border-radius: 2px;
        background: #C0C0C0;
        -webkit-box-shadow: 2px 2px 8px 2px rgba(0, 0, 0, 0.15);
        -moz-box-shadow: 2px 2px 8px 2px rgba(0, 0, 0, 0.15);
        box-shadow: 2px 2px 8px 2px rgba(0, 0, 0, 0.15);
        min-width: 325px;
        margin: 0 auto;
      }
      
      .inputty {
        width: 75%;
        height: 45px;
        font-size: 22px;
        font-family: 'work sans';
      }
      <!DOCTYPE html>
      <html lang="en">
      
      <head>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <link href="https://fonts.googleapis.com/css?family=Work+Sans" rel="stylesheet">
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>jQuery UI Sortable - Default functionality</title>
        <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 type="text/javascript" src="java4.js"></script>
        <link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="style.css">
      
      
      </head>
      
      <body>
        <div class="center">
      
          <div class="optionBox" style="position: relative;">
            <button class="addtask" id="add" class="center">+  ADD TASK</button>
            <div id="sortable" class="ul" class="center">
      
      
            </div>
          </div><br>
        </div>
      </body>
      
      </html>

      【讨论】:

      • 谢谢。你是学者,也是圣人。如此简单却正是我所需要的!
      • @WesCreations 乐于助人! :-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多