0

I have draggable div which dragging above .outerDiv with text content. How can I get text from .outerDiv which got into the borders of draggable div ?

$(".outerDiv .isStore").draggable({ containment: ".outerDiv" }).resizable({ containment: ".outerDiv" });
.isStore
{
  width: 20px;
  height: 20px;
  background-color: red;
  }
.outerDiv
{
  width: 160px;
  height: 160px;
  background-color: #ccc;
  }
.ui-resizable {
position: absolute !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>
<div class='outerDiv'><div class='isStore'></div> sdfsdfsd
<br>
first
<br>
second
<br>
third
</div>

Edit: I want code that takes part of the text, under .isStore, from .outerDiv and puts it in .isStore after dragging.

What I want

Ivan Ivanov
  • 515
  • 1
  • 5
  • 20

3 Answers3

2

var is_colliding = function( $div1, $div2 ) {
 // Div 1 data
 var d1_offset             = $div1.offset();
 var d1_height             = $div1.outerHeight( true );
 var d1_width              = $div1.outerWidth( true );
 var d1_distance_from_top  = d1_offset.top + d1_height;
 var d1_distance_from_left = d1_offset.left + d1_width;

 // Div 2 data
 var d2_offset             = $div2.offset();
 var d2_height             = $div2.outerHeight( true );
 var d2_width              = $div2.outerWidth( true );
 var d2_distance_from_top  = d2_offset.top + d2_height;
 var d2_distance_from_left = d2_offset.left + d2_width;

 var not_colliding = ( d1_distance_from_top < d2_offset.top || d1_offset.top > d2_distance_from_top || d1_distance_from_left < d2_offset.left || d1_offset.left > d2_distance_from_left );

 // Return whether it IS colliding
 return ! not_colliding;
};

$(function(){

   $(".outerDiv .isStore")
      .draggable(
         {
        containment: ".outerDiv",
    drag: function() {
       $('.targetElem').each(function(){
          $isStore = $('.isStore');
          if (is_colliding($isStore , $(this))) {
        var elemName = $(this).text();
   if ($isStore.text().indexOf(elemName) == -1) {
      $isStore.append(elemName+'<br>');
   }
     }
  });
         }
         }
     )
     .resizable({ containment: ".outerDiv" });
});
.targetElem {background:yellow;}
.isStore
{
  width: 20px;
  height: 20px;
  background-color: red;
  }
.outerDiv
{
  width: 160px;
  height: 160px;
  background-color: #ccc;
  }
.ui-resizable {
position: absolute !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>
<div class='outerDiv'><div class='isStore'></div>
<br>
<br>
<br>
<span class="targetElem">first</span><br>
<br>
<span class="targetElem">second</span><br>
<br>
<span class="targetElem">third</span>
</div>

I used code that detects if two divs are colliding: http://stackoverflow.com/questions/5419134/how-to-detect-if-two-divs-touch-with-jquery

Bahman Parsa Manesh
  • 2,314
  • 3
  • 16
  • 32
Luke Wenke
  • 1,149
  • 2
  • 23
  • 43
0

Here is a silly example; I just created a draggable div which contain both the text and the isStore box:

$(".outerDiv .content").draggable({ containment: ".outerDiv" });
$(".outerDiv .isStore").resizable({ containment: ".outerDiv" });
.isStore
{
  width: 20px;
  height: 20px;
  background-color: red;
  }
.outerDiv
{
  width: 160px;
  height: 160px;
  background-color: #ccc;
  }
.ui-resizable {
position: absolute !important;
}
.content {
  width: 80px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>
<div class='outerDiv'><div class="content"><div class='isStore'></div> sdfsdfsd
<br>
first
<br>
second
<br>
third
</div></div>
0

Is this what you mean? (putting the text inside the red draggable/resizable div) Or do you want code that dynamically takes the code from outerDiv and puts it in isStore? Note I used "overflow:hidden" in the style for isStore so that it doesn't show outside of the div.

$(".outerDiv .isStore").draggable({ containment: ".outerDiv" }).resizable({ containment: ".outerDiv" });
.isStore
{
  width: 20px;
  height: 20px;
  background-color: red;
  overflow: hidden;
  }
.outerDiv
{
  width: 160px;
  height: 160px;
  background-color: #ccc;
  }
.ui-resizable {
position: absolute !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>
<div class='outerDiv'><div class='isStore'>sdfsdfsd
<br>
first
<br>
second
<br>
third</div> 
</div>
Luke Wenke
  • 1,149
  • 2
  • 23
  • 43