There is a little div inside a big div . Through JS, Drag'n'Drop is applied to a small div . It is necessary to limit the area of movement of the smaller block.
But if you try to sharply move the white block to the left or up, it will “stick” near the border. How to fix it?
var small = document.getElementById('small'); small.onmousedown = function(e) { move(e); function move(e) { if (e.pageX > 0) { small.style.left = e.pageX + 'px'; } if (e.pageY > 0) { small.style.top = e.pageY + 'px'; } } document.onmousemove = function(e) { move(e); } small.onmouseup = function() { document.onmousemove = null; small.onmouseup = null } } #big { width: 12cm; height: 12cm; background-color: #DDDDDD; } #small { position: absolute; left: 0.75cm; top: 0.75cm; width: 4cm; height: 4cm; background-color: #FFFFFF; } <div id="big"> <div id="small"></div> </div>
divclose to the border. - Andrei