var dragFix, startFix, stopFix; window.myApp = { layout: { zoomScale: 0.8 }, draggable: { _uiHash: function() { return { helper: this.helper, position: this.position, originalPosition: this.originalPosition, offset: this.positionAbs, containment: this.containment }; } }, resizable: { _change: { e: function(event, dx) { return { width: this.originalSize.width + dx / myApp.layout.zoomScale }; }, w: function(event, dx) { var cs, sp; cs = this.originalSize; sp = this.originalPosition; return { left: sp.left + dx, width: cs.width - dx / myApp.layout.zoomScale }; }, n: function(event, dx, dy) { var cs, sp; cs = this.originalSize; sp = this.originalPosition; return { top: sp.top + dy, height: cs.height - dy / myApp.layout.zoomScale }; }, s: function(event, dx, dy) { return { height: this.originalSize.height + dy / myApp.layout.zoomScale }; } }, plugins: { containment: { resize: function(event) { var ce, co, continueResize, cop, cp, hoset, isOffsetRelative, isParent, o, pRatio, that, woset; woset = void 0; hoset = void 0; isParent = void 0; isOffsetRelative = void 0; that = $(this).resizable("instance"); o = that.options; co = that.containerOffset; cp = that.position; pRatio = that._aspectRatio || event.shiftKey; cop = { top: 0, left: 0 }; ce = that.containerElement; continueResize = true; if (ce[0] !== document && /static/.test(ce.css("position"))) { cop = co; } if (cp.left < (that._helper ? co.left : 0)) { that.size.width = that.size.width + (that._helper ? that.position.left - co.left : that.position.left - cop.left); if (pRatio) { that.size.height = that.size.width / that.aspectRatio; continueResize = false; } that.position.left = (o.helper ? co.left : 0); } if (cp.top < (that._helper ? co.top : 0)) { that.size.height = that.size.height + (that._helper ? that.position.top - co.top : that.position.top); if (pRatio) { that.size.width = that.size.height * that.aspectRatio; continueResize = false; } that.position.top = (that._helper ? co.top : 0); } isParent = that.containerElement.get(0) === that.element.parent().get(0); isOffsetRelative = /relative|absolute/.test(that.containerElement.css("position")); if (isParent && isOffsetRelative) { that.offset.left = that.parentData.left + that.position.left; that.offset.top = that.parentData.top + that.position.top; } else { that.offset.left = that.element.offset().left; that.offset.top = that.element.offset().top; } woset = Math.round(Math.abs(that.sizeDiff.width + (that._helper ? that.offset.left - cop.left : (that.offset.left - co.left) / myApp.layout.zoomScale))); hoset = Math.round(Math.abs(that.sizeDiff.height + (that._helper ? that.offset.top - cop.top : (that.offset.top - co.top) / myApp.layout.zoomScale))); if (woset + that.size.width >= that.parentData.width) { that.size.width = that.parentData.width - woset; if (pRatio) { that.size.height = that.size.width / that.aspectRatio; continueResize = false; } } if (hoset + that.size.height >= that.parentData.height) { that.size.height = that.parentData.height - hoset; if (pRatio) { that.size.width = that.size.height * that.aspectRatio; continueResize = false; } } if (!continueResize) { that.position.left = that.prevPosition.left; that.position.top = that.prevPosition.top; that.size.width = that.prevSize.width; that.size.height = that.prevSize.height; } } } } } }; $.ui.draggable.prototype._uiHash = myApp.draggable._uiHash; $.extend($.ui.resizable.prototype._change, myApp.resizable._change); startFix = function(event, ui) { ui.containment[2] *= myApp.layout.zoomScale; return ui.containment[3] *= myApp.layout.zoomScale; }; stopFix = function(event, ui) { ui.containment[2] /= myApp.layout.zoomScale; return ui.containment[3] /= myApp.layout.zoomScale; }; dragFix = function(event, ui) { var deltaX, deltaY; deltaX = ui.position.left - ui.originalPosition.left; deltaY = ui.position.top - ui.originalPosition.top; ui.position.left = ui.originalPosition.left + deltaX / myApp.layout.zoomScale; return ui.position.top = ui.originalPosition.top + deltaY / myApp.layout.zoomScale; }; $("#draggable").resizable({containment: '#parent'}).draggable( { containment: '#parent', start: startFix, drag: dragFix, stop: stopFix } ); var plugin, r_inst, _i, _len, _ref; r_inst = $('#draggable').resizable('instance'); _ref = r_inst.plugins.resize; for (_i = 0, _len = _ref.length; _i < _len; _i++) { plugin = _ref[_i]; if (plugin[0] === 'containment') { plugin[1] = myApp.resizable.plugins.containment.resize; } }
<body> <div id="parent"> <div id="draggable"> Hello! </div> </div> </body>
#parent { background-color: blue; width: 400px; height: 400px; transform: scale(0.8); -moz-transform: scale(0.8); -webkit-transform: scale(0.8); transform-origin: center top 0px; -moz-transform-origin: center top 0px; -webkit-transform-origin: center top 0px; } #draggable { background-color: rgba(0,0,0,0.5); border: 1px solid black; width: 50px; height: 50px; color: white; }