【问题标题】:Draggable element scrolling another div with overflow可拖动元素滚动另一个 div 并溢出
【发布时间】:2017-11-15 09:42:17
【问题描述】:

您好,我对可拖动事件有疑问。 我无法使用助手滚动另一个 div。

这是示例代码,我写道:

$('.test1').draggable({
  helper: 'clone',
  scroll: false,
  appendTo: '.scrolled-box',
  containment: '.test2',
  start: function(e, ui) {
    $(ui.helper).css({
      'opacity': '0.6'
    });
  }
});
.container {
  width: 250px;
  height: 100px;
  background-color: #ffecc9;
  padding: 10px;
  border-radius: 3px;
  border: 1px solid #333;
}

.test1 {
  width: 200px;
  height: 20px;
  border: 1px solid #333;
  background: #33cccc;
  border-radius: 3px;
  margin: auto;
}

.test1>p {
  font-size: 12px;
  font-weight: bold;
  margin: 0 auto;
  display: block;
  width: 100%;
  text-align: center;
  color: #333;
  padding: 2px 0;
}

.test2 {
  margin-top: 15px;
  width: 250px;
  height: 60px;
  overflow: auto;
  background-color: #333;
  border-radius: 3px;
  color: white;
}

.scrolled-box {
  height: 500px;
  width: 100%;
  text-align: center;
  position: relative;
}

span {
  position: absolute;
  bottom: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="test1">
    <p>
      drag me
    </p>
  </div>
  <div class="test2">
    <div class="scrolled-box">
      <span>scrolled box</span>
    </div>
  </div>
</div>

请看这个:JsFiddle

我做错了什么,我不知道是什么。 请帮助我通过可拖动(jquery ui)提升我的经验。 我正在寻找没有额外插件的解决方案。只是 jQuery Ui。

【问题讨论】:

    标签: javascript jquery jquery-ui jquery-ui-draggable


    【解决方案1】:

    您还需要处理droppable 事件

    $('.test1').draggable({
      scroll: true,
      helper: 'clone',
      revert: true,
      appendTo: '.scrolled-box',
      containment: '.test2',
      start: function (e, ui) {
      	$(ui.helper).css({'opacity': '0.6'});
      }
    });
    
    $('.test2').droppable({
      accept: ".test1",
      drop: function( event, ui ) {
    				ui.helper.clone().appendTo('.scrolled-box');
        }
    });
    .container {
      width: 250px;
      height: 300px;
      background-color: #ffecc9;
      padding: 10px;
      border-radius: 3px;
      border: 1px solid #333;
    }
    .test1 {
      width: 200px;
      height: 20px;
      border: 1px solid #333;
      background: #33cccc;
      border-radius: 3px;
      margin: auto;
    }
    .test1 > p {
      font-size: 12px;
      font-weight: bold;
      margin: 0 auto;
      display: block;
      width: 100%;
      text-align: center;
      color: #333;
      padding: 2px 0;
    }
    .test2 {
      margin-top: 15px;
      width: 250px;
      height: 160px;
      overflow: auto;
      background-color: #333;
      border-radius: 3px;
      color: white;
      position: relative;
    }
    .scrolled-box {
      height: 600px;
      width: 100%;
      text-align: center;
      position: relative;
    }
    span {
      position: absolute;
      bottom: 10px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    <div class="container">
      <div class="test1">
        <p>
          drag me
        </p>
      </div>
      <div class="test2">
        <div class="scrolled-box">
          <span>scrolled box</span>
        </div>
      </div>
    </div>

    【讨论】:

    • 在我的 JS Fiddle 中测试你的代码。它对我不起作用。
    • 检查这个fiddle。会有帮助的!
    • 当您要求使用助手滚动另一个 div 时,请检查此fiddle
    • 你改变了什么? CSS? jquery 我明白了。你能写出你改变了什么吗?
    • 父 div(.test2) 需要有 position : relative 作为其可放置的孩子
    猜你喜欢
    • 2013-08-06
    • 2010-12-21
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    相关资源
    最近更新 更多