Manu Padmanabhan
Manu Padmanabhan

Reputation: 555

How to get the start value and end value of a div after dragging

Hi i have a div which is draggable .My requirement is 1. I want the start value and end value of the div after dragging in a text box 2.Now its is only drag in right side only i want it drag also from left side of the div i tried some steps but it is not perfect because the value is not displaying and the dragging from left is not working middle table is my parent div

   $(function () {
        var container = $('.middletablediv'),
       base = null,
       handle = $('.handle'),
       isResizing = false,
   screenarea = screen.width;

        handle.on('mousedown', function (e) {
            base = $(this).closest(".scalebar");
            isResizing = true;
            lastDownX = e.clientX;
            offset = $(this).offset();
            xPos = offset.left;

        });

        $(document).on('mousemove', function (e) {
            // we don't want to do anything if we aren't resizing.
            if (!isResizing)
                return;

            p = parseInt(e.clientX - base.offset().left),
            // l = parseInt(p * (3 / 11));
            base.css('width', p);
            k = parseInt(xPos - base.offset().left);


            $("#startvalue").value(k)
            $("#stopvalue").value(p)
        }).on('mouseup', function (e) {
            // stop resizing
            isResizing = false;




        });
        });
.handle{
    position: absolute;

    top:1px;
    right: 0;
    width: 10px;
    height: 5px;
    cursor: w-resize;
    }
  .middletablediv{
   float:left;
  width:35%;
   
}
  .scalebar{
    margin-top: 13px;
    height: 7px;
    position: relative;
   width:20px;
   background-color: green; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="middletablediv">
    <div id="newvalue1" class="scalebar">
        <div class="handle" style="left:0"></div>  <div class="handle"></div>
    </div>
</div>
    <input id="startvalue" type="text">startvalue</input>
    <input id="stopvalue" type="text" />stopvalue</input>

how i solve this issue

Upvotes: 0

Views: 173

Answers (1)

Angelos Chalaris
Angelos Chalaris

Reputation: 6747

You should use val() instead of value(). Also, the way dragging works the end value can be smaller than the start value, so I added a couple of things to handle that problem in a simple way (just switch values). Finally, to drag from the left, you should handle the left dragging differently, so I gave the left handle a unique id and also padded the whole parent div a bit to make it more apparent.

$(function () {
        var container = $('.middletablediv'),
       base = null,
       handle = $('.handle'),
       isResizing = false,
       isLeftDrag = false;
   screenarea = screen.width;

        handle.on('mousedown', function (e) {
            base = $(this).closest(".scalebar");
            isResizing = true;
          if($(this).attr('id')=='lefthandle')isLeftDrag=true;
          else isLeftDrag=false;
            lastDownX = e.clientX;
            offset = $(this).offset();
            xPos = offset.left;

        });

        $(document).on('mousemove', function (e) {
            // we don't want to do anything if we aren't resizing.
            if (!isResizing)
                return;
            if(isLeftDrag){
              
              p = parseInt(base.offset().left - e.clientX);
            k = parseInt(base.offset().left - xPos);
              
              base.css('margin-left',-p);
              base.css('width',p); 
              }
          else{
            p = parseInt(e.clientX - base.offset().left),
            // l = parseInt(p * (3 / 11));
            base.css('width', p);
            k = parseInt(xPos - base.offset().left);
            }
            //if(k>p){var temp = k; k = p; p = temp;}

            $("#startvalue").val(k)
            $("#stopvalue").val(p)
        }).on('mouseup', function (e) {
            // stop resizing
            isResizing = false;




        });
        });
.handle{
    position: absolute;

    top:1px;
    right: 0;
    width: 10px;
    height: 5px;
    cursor: w-resize;
    }
  .middletablediv{
   float:left;
  width:35%;
   
}
  .scalebar{
    margin-top: 13px;
    height: 7px;
    position: relative;
   width:20px;
   background-color: green; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="middletablediv" style="padding-left:100px; overflow:visible;">
    <div id="newvalue1" class="scalebar">
        <div class="handle"id="lefthandle" style="left:0"></div>  <div class="handle"></div>
    </div>
</div><br><br>
    <input id="startvalue" type="text">startvalue</input>
    <input id="stopvalue" type="text" />stopvalue</input>

Upvotes: 1

Related Questions