$('.box').each(function() { $('.box').draggable({containment: '#boxid'});}); $('.box').each(function() { $('.box').resizable();}); let boxH = $("#boxid").height(), boxW = $("#boxid").width(), doit; $(window).on('resize', function() { clearTimeout(doit); doit = setTimeout(function() { let box = $("#boxid"), newH = box.height(), newW = box.width(); if (newH != boxH) { $('.box').each(function() { let prop = newH / boxH, self = $(this), sT = Number(self.css('top').slice(0,-2)), sH = Number(self.css('height').slice(0,-2)); self.css({'top': sT * prop}); self.css({'height': sH * prop}); console.log("TOP/LEFT", self.css('top'), self.css('left')); }); } if (newW != boxW) { $('.box').each(function() { let prop = newW / boxW, self = $(this), sL = Number(self.css('left').slice(0,-2)), sW = Number(self.css('width').slice(0,-2)); self.css({'left': sL * prop}); self.css({'width': sW * prop}); console.log("TOP/LEFT", self.css('top'), self.css('left')); }); } boxH = newH; boxW = newW; }, 300); });
#boxid { height: 750px; width: 50%; margin: 0 auto; border: 5px dotted #292929; } .box { background:rgb(107, 193, 243); cursor:move; position:absolute; }
<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <div class="row"> <div id="boxid" class="col-xs-12"> <div id="id1" class="box" style="top:50px; left:50px; width:50px; height:50px;"> <p id="top"></p> <p id="left"></p> <p id="height"></p> <p id="width"></p> </div> <div id="id2" class="box" style="top:50px; left:50px; width:50px; height:50px;"> <p id="top"></p> <p id="left"></p> <p id="height"></p> <p id="width"></p> </div> </div> </div>