从左侧拖动div不能更好地工作

时间:2016-08-11 08:08:19

标签: javascript jquery

嗨,我有一个div,它是左侧和右侧的拖曳式。现在从右侧拖动是完美的。我在左侧拖动时出现问题。当我尝试从左侧拖动div时div的宽度完全消失了,只有拖动值得到宽度我希望从我的div的左侧增加或减少我的宽度

$(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;}

          
        }).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>
    
  

1 个答案:

答案 0 :(得分:1)

检查一下,这是你想要的吗

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

        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 + oldWidth )); 
              }
          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;}

          
        }).on('mouseup', function (e) {
            // stop resizing
            isResizing = false;
            oldWidth = base.width();



        });
        });
&#13;
.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; }
&#13;
<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>
    
  
&#13;
&#13;
&#13;