Search code examples
javascriptjqueryresizable

jquery resizable 1px mismatch error


I am trying to use jquery resizable handlers with absolute position square divs

but when I click the "north" and "west" button(only this two direction) and drag just a little for resizing, the box itself became smaller by 1 or 2px

in north handler case, like (top:100px,height:100px => top:99px,height:99px)

what did I do wrong??

element.resizable({
    minWidth: 1,
    minHeight: 1,
    handles:{
        'n': '.ui-resizable-n',
        'w': '.ui-resizable-w',
        'e': '.ui-resizable-e',
        's': '.ui-resizable-s'
    }
})

Here's a demo link!

when I drag "keeper" item to "screen" and move north resize handler little, the square makes problem


Solution

  • Please change box-sizing: border-box; to box-sizing: content-box;.

    https://jsfiddle.net/6h02cmvf/

    $(document).ready(function() {
    		$('#container > .ui-element-libraries-container > .ui-element-container').draggable({
    			opacity: 0.5,
    			helper: "clone",
    			appendTo: '#full',
    			cursor: "move",
    			revert: true
    		})
    
    		$('#screen').droppable({
    			drop: (event,ui)=>{
    				let layoutOffset=$('#screen').offset()
    				let element=ui.helper.clone().css({
    					'position': 'absolute',
    					'opacity': 1,
    					'left': (ui.position.left-layoutOffset.left)+'px',
    					'top': (ui.position.top-layoutOffset.top)+'px',
    				})
    				element.toggleClass('ui-element-container ui-element')
    				element.empty()
    				element.append("<div class='ui-resizable-handle ui-resizable-n'></div>")
    				element.append("<div class='ui-resizable-handle ui-resizable-s'></div>")
    				element.append("<div class='ui-resizable-handle ui-resizable-e'></div>")
    				element.append("<div class='ui-resizable-handle ui-resizable-w'></div>")
    				element.appendTo('#screen')
    				element.resizable({
    					minWidth: 1,
    					minHeight: 1,
    					handles:{
    						'n': '.ui-resizable-n',
    						'w': '.ui-resizable-w',
    						'e': '.ui-resizable-e',
    						's': '.ui-resizable-s'
    					}
    				})
    				ui.helper.remove()
    			}
    		})
    	})
    .pane{
      color: black;
      background-color: white;
      position: absolute;
      display: block;
      overflow: hidden;
      border: 2px solid black;
    }
    #container{
      margin: 0px;
      left: 300px;
      right: auto;
      top: 0px;
      bottom: 0px;
      width: 300px;
      height: 500px;
    }
    #screen{
      margin: 0px;
      left: 0px;
      right: auto;
      top: 0px;
      bottom: 0px;
      width: 300px;
      height: 500px;
    }
    .ui-element-libraries-container{
      width:100%;
    }
    .ui-element-container{
      color: black;
      background-color: yellow;
      border: 1px solid black;
      margin-left: -1px;
      margin-top: -1px;
      width: 33.3%;
      min-width: 80px;
      max-width: 90px;
      height: 100px;
      box-sizing: border-box;
      text-align: center;
      line-height: 12px;
      float:left;
      font-size: 11px;
      position: relative;
      overflow: hidden;
      cursor: move;				
    }
    .ui-element{
      color: black;
      background-color: white;
      border: 1px solid black;
      margin-left: -1px;
      margin-top: -1px;
      width: 100px;
      height: 100px;
      box-sizing: content-box;
      text-align: center;
      line-height: 12px;
      float:left;
      font-size: 11px;
      position: relative;
      cursor: move;	
    }
    .ui-resizable-handle{
      position: absolute;
      width: 7px;
      height: 7px;
      border: 1px solid blue;
      background: white;
      display: block;
    }
    .ui-resizable-n{
      top: -4px;
      bottom: auto;
      left: 50%;
      right: auto;
      margin-left: -4px;
      cursor: ns-resize;		
    }
    .ui-resizable-s{
      top: auto;
      bottom: -4px;
      left: 50%;
      right: auto;
      margin-left: -4px;
      cursor: ns-resize;		
    }
    .ui-resizable-e{
      top: 50%;
      bottom: auto;
      left: auto;
      right: -4px;
      margin-top: -4px;
      cursor: ew-resize;
    }
    .ui-resizable-w{
      top: 50%;
      bottom: auto;
      left: -4px;
      right: auto;
      margin-top: -4px;
      cursor: ew-resize;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
    
    <div id="full">
      <div id="screen" class="pane">
        screen
      </div>
      <div id="container" class="pane">
        keeper
        <div class="ui-element-libraries-container">
          <div class="ui-element-container">
            <div>
              b
            </div>
          </div>
        </div>
      </div>
    </div>