2

Hi I have problem with draggable event. I can't scroll another div with helper.

This is example code, I wrote:

$('.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>

Please see this: JsFiddle

I did something wrong, and I don't know what. Please help me to lvl up my expirience with draggable (jquery ui). Im looking for solution without extra plugins. Just jQuery Ui.

1 Answer 1

2

You need to handle droppable event as well

$('.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>

Sign up to request clarification or add additional context in comments.

8 Comments

Test your code inside my JS Fiddle. It doesn't work for me.
Check this fiddle. It will help!
As you asked to scroll another div with helper, Check this fiddle.
what did you changed? Css? jquery i see. Could you write what did you changed?
parent div(.test2) needs to have position : relative as of its droppable child
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.