Hello.
There is a js code with which you can move elements, but I don’t understand how to limit the area of movement of an element, for example, within a specific block? Please tell me how to do this in pure javascript.
Hello.
There is a js code with which you can move elements, but I don’t understand how to limit the area of movement of an element, for example, within a specific block? Please tell me how to do this in pure javascript.
Element.prototype.makeDraggable=function(){ var o=this o.onmousedown=function(e){ var offsetX=e.pageX-parseInt(o.style.left) var offsetY=e.pageY-parseInt(o.style.top) document.onmousemove=function(e) { o.style.left=Math.max(Math.min(e.pageX-offsetX,o.parentNode.clientWidth-o.clientWidth),0)+'px' o.style.top=Math.max(Math.min(e.pageY-offsetY,o.parentNode.clientHeight-o.clientHeight),0)+'px' } document.onmouseup = function(e) { document.onmousemove=o.onmouseup=null } } o.ondragstart = function(){return 0} } document.getElementById('object1').makeDraggable() #parent{ position:absolute; top:10px; left:10px; background:yellow; width:300px; height:300px; } #object1{ width:100px; height:100px; position:absolute; background:green; } <div id="parent"> <div id="object1" style="left:10px;top:10px;"></div> </div> If you are using jQuery UI drag and drop, then here is an example from this manual .
For binding to a specific container, the option with the value is responsible:
containment: parent Source: https://ru.stackoverflow.com/questions/357154/
All Articles