Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make a function to find the coordinates of an element regardless of its positioning?
    primarykey
    data
    text
    <p>I'm making a drag and drop engine in JavaScript, and I don't know how to set the correct position of the dragObj because it changes depending on the parent element's positioning type (Does the dragObj also change depending on its parent's "parent element" ect.?).</p> <p>So, my dragObj looks like this:</p> <pre><code>function makeObj(event) { var obj = new Object(); var e = event.target; obj.element = e; obj.boundElement = null; while(e = e.parentNode) { if(~e.className.search(/bound/)) { //if(/bound/.test(e.className)) { obj.boundElement = e; break; } } if(obj.boundElement == null) obj.boundElement = document.body; // I would like to find the correct minimum bounds with findPos(); however, I need // findPos() to work with every type of positioning (absolute, relatice, ect.) //var elemPos = findPos(obj.boundElement); //obj.minBoundX = elemPos.x; //obj.minBoundY = elemPos.y; obj.minBoundX = obj.boundElement.offsetLeft + obj.boundElement.offsetWidth - obj.element.offsetWidth; obj.minBoundY = obj.boundElement.offsetTop + obj.boundElement.offsetHeight - obj.element.offsetHeight; obj.maxBoundX = obj.boundElement.offsetLeft; obj.maxBoundY = obj.boundElement.offsetTop; setHelperBoxPos(obj); obj.posX = event.clientX - obj.element.offsetLeft; obj.posY = event.clientY - obj.element.offsetTop; return obj; } </code></pre> <p>So, when I make a dragObj, I also set its "position" and its <code>bounding element</code>. In a comment portion right before I set the <code>.minBoundX</code> and <code>.minBoundY</code> attributes I explain how I would like to set them; however, it doesn't work because the findPos() function doesn't work.</p> <p>Here is the findPos() function:</p> <pre><code>function findPos(obj) { // Donated by `lwburk` on StackOverflow var curleft = curtop = 0; if (obj.offsetParent) { do { curleft += obj.offsetLeft; curtop += obj.offsetTop; } while (obj = obj.offsetParent); return { x: curleft, y: curtop }; } } </code></pre> <p>I believe this function works if the <code>bounding element</code> has <code>position: absolute;</code> set, but I want the user to be able to set its positioning type. Also, the <code>bounding element</code> is set by the <code>.bound</code> class, and the dragObj is set by the <code>.drag</code> class.</p> <p>Here's the HTML:</p> <pre><code>&lt;div id="min" class="helper-box" style="border: 1px solid blue;"&gt;&lt;/div&gt; &lt;div id="max" class="helper-box" style="border: 1px solid red;"&gt;&lt;/div&gt; &lt;div id="center"&gt; &lt;h1&gt;Hello World! &lt;hr /&gt;&lt;/h1&gt; &lt;div id="box" class="bound"&gt; &lt;p class="drag square"&gt; One &lt;/p&gt; &lt;p class="drag square"&gt; Two &lt;/p&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>And here is the CSS:</p> <pre><code>@charset "utf-8"; /* CSS Document */ * { padding: 0px; margin: 0px; } body { background-color:#DFDFDF; } .drag { position: absolute; -webkit-user-select: none; -moz-user-select: none; user-select: none; } .bound { ; } .square { width: 100px; height: 100px; background: #1047A9; cursor:move; border-radius: 25px; -moz-border-radius: 25px; } #center { width: 500px; margin: auto; margin-top: 50px; background-color: #29477F; color: #E8E8E8; text-align: center; border-radius: 25px; -moz-border-radius: 25px; } #box { background-color: #009EBE; height: 275px; border-radius: 0 0 25px 25px; -moz-border-radius: 0 0 25px 25px; opacity: 1.0; } .helper-box { position: absolute; width: 5px; height: 5px; } </code></pre> <p>And here is the entire engine:</p> <pre><code>// JavaScript Document var dragObj; document.addEventListener("mousedown", down, false); function down(event) { if(~event.target.className.search(/drag/)) { dragObj = makeObj(event); dragObj.element.style.zIndex="100"; document.addEventListener("mousemove", freeMovement, false); } } function freeMovement(event) { if (typeof(dragObj.element.mouseup) == "undefined") document.addEventListener("mouseup", drop, false); //Prevents redundantly adding the same event handler repeatedly dragObj.element.style.left = Math.max(dragObj.maxBoundX, Math.min(dragObj.minBoundX, event.clientX - dragObj.posX)) + "px"; dragObj.element.style.top = Math.max(dragObj.maxBoundY, Math.min(dragObj.minBoundY, event.clientY - dragObj.posY)) + "px"; } function drop() { dragObj.element.style.zIndex="1"; document.removeEventListener("mousemove", freeMovement, false); document.removeEventListener("mouseup", drop, false); //alert("DEBUG_DROP"); } function makeObj(event) { var obj = new Object(); var e = event.target; obj.element = e; obj.boundElement = null; while(e = e.parentNode) { if(~e.className.search(/bound/)) { //if(/bound/.test(e.className)) { obj.boundElement = e; break; } } if(obj.boundElement == null) obj.boundElement = document.body; // I would like to find the correct minimum bounds with findPos(); however, I need // findPos() to work with every type of positioning (absolute, relatice, ect.) //var elemPos = findPos(obj.boundElement); //obj.minBoundX = elemPos.x; //obj.minBoundY = elemPos.y; obj.minBoundX = obj.boundElement.offsetLeft + obj.boundElement.offsetWidth - obj.element.offsetWidth; obj.minBoundY = obj.boundElement.offsetTop + obj.boundElement.offsetHeight - obj.element.offsetHeight; obj.maxBoundX = obj.boundElement.offsetLeft; obj.maxBoundY = obj.boundElement.offsetTop; setHelperBoxPos(obj); obj.posX = event.clientX - obj.element.offsetLeft; obj.posY = event.clientY - obj.element.offsetTop; return obj; } function findPos(obj) { // Donated by `lwburk` on StackOverflow var curleft = curtop = 0; if (obj.offsetParent) { do { curleft += obj.offsetLeft; curtop += obj.offsetTop; } while (obj = obj.offsetParent); return { x: curleft, y: curtop }; } } function setHelperBoxPos(obj) { var minBox = document.getElementById('min'); minBox.style.left = obj.minBoundX + 'px'; minBox.style.top = obj.minBoundY + 'px'; var maxBox = document.getElementById('max'); maxBox.style.left = obj.maxBoundX + 'px'; maxBox.style.top = obj.maxBoundY + 'px'; } </code></pre> <p>I have also made a jsfiddle for your convenience: <a href="http://jsfiddle.net/2XGhK/" rel="nofollow">http://jsfiddle.net/2XGhK/</a></p> <p><strong>So, how do I make a findPos() function which allows for different kinds of positioning. Will I need to make another findPos() function to allow the dragObj to be any kind of positioning as well?</strong></p> <p><strong>Important</strong> Please do not recommend using a library (unless you suggest looking at it for tips on how they deal with positioning).The reason is that I am just learning the language and building things helps me do just that. What's the point in learning a library before I even understand the language?</p> <p>Most importantly, I greatly appreciate all of your help. Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload