1

Hello everyone this is my first question so I might have done it wrong. What I am trying to achieve is is have multiple <aside> elements all with the same ID Class and of course Tag be able to be moved anywhere on the screen with drag and drop characteristics. I have found a JSFiddle demonstrating the code I am basing this around that uses one aside element with the ability to be moved anywhere, but will not work when multiple elements are used. The code controlling the movement is here:

function drag_start(event) {
var style = window.getComputedStyle(event.target, null);
event.dataTransfer.setData("text/plain",
(parseInt(style.getPropertyValue("left"),10) - event.clientX) + ',' + 

(parseInt(style.getPropertyValue("top"),10) - event.clientY));
} 
function drag_over(event) { 
    event.preventDefault(); 
    return false; 
} 
function drop(event) { 
    var offset = event.dataTransfer.getData("text/plain").split(',');
    var dm = document.getElementById('dragme');
    dm.style.left = (event.clientX + parseInt(offset[0],10)) + 'px';
    dm.style.top = (event.clientY + parseInt(offset[1],10)) + 'px';
    event.preventDefault();
    return false;
} 
var dm = document.getElementById('dragme'); 
dm.addEventListener('dragstart',drag_start,false); 
document.body.addEventListener('dragover',drag_over,false); 
document.body.addEventListener('drop',drop,false); 

The part I am having trouble with is the drop system which requires the elements to have individual ids. I also need to have all aside elements have to same id and I don't want to use classes. I have tried and thought and searched the web for the past three days with no luck of finding an answer as all link back to this same code. I have looked into getting the index of the last element clicked but cannot find a way to get all elements with the same ID. Thanks in advanced - bybb Update: Have broken the dnd system need help with that:

var dragindex = 0;
$(document).ready(function(){
    $("aside").click(function(){
        dragindex = $(this).index();
    });
});
function drag_start(event) {
var style = window.getComputedStyle(event.target, null);
event.dataTransfer.setData("text/plain",
(parseInt(style.getPropertyValue("left"),10) - event.clientX) + ',' + (parseInt(style.getPropertyValue("top"),10) - event.clientY));
} 
function drag_over(event) { 
event.preventDefault(); 
return false; 
} 
function drop(event) { 
var offset = event.dataTransfer.getData("text/plain").split(',');
var dm = document.getElementById('#winborder');
dm.style.left = (event.clientX + parseInt(offset[0],10)) + 'px';
dm.style.top = (event.clientY + parseInt(offset[1],10)) + 'px';
event.preventDefault();
return false;
} 
function makewindow(content, storevar) {
    storevar = document.createElement('aside');
    storevar.setAttribute("dragable", "true");
    storevar.setAttribute("id", "winborder");
    var content = document.createElement('div');
    content.setAttribute("id", "wincontent");
    storevar.appendChild(content);
    document.body.appendChild(storevar);
    storevar.addEventListener('dragstart',drag_start,false);
    document.body.addEventListener('dragover',drag_over,false); 
    document.body.addEventListener('drop',drop,false); 
}

Have tried with and without '#' on getelementbyid

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
xnoe
  • 13
  • 1
  • 6

2 Answers2

0

The following code will allow drag and drop; I'm sorry it doesn't follow your previous coding style.

Here is a JSFiddle showing it in action: https://jsfiddle.net/6gq7u8Lc/

    document.getElementById("dragme").onmousedown = function(e) {
        this.prevX = e.clientX;
        this.prevY = e.clientY;
        this.mouseDown = true;
    }
    document.getElementById("dragme").onmousemove = function(e) {
        if(this.mouseDown) {
            this.style.left = (Number(this.style.left.substring(0, this.style.left.length-2)) + (e.clientX - this.prevX)) + "px";
            this.style.top = (Number(this.style.top.substring(0, this.style.top.length-2)) + (e.clientY - this.prevY)) + "px";
        }
        this.prevX = e.clientX;
        this.prevY = e.clientY;
    }
    document.getElementById("dragme").onmouseup = function() {
        this.mouseDown = false;
    }

On a side note, you won't be able to give them all the same id. The DOM doesn't support such a thing. If you want to add multiple elements of the same type, I would suggest a 'container' parent div, adding the elements as children of the div, and iterating through the .children attribute to access each.

Andrue Anderson
  • 664
  • 4
  • 17
  • Attempted code shows the faded version when drag, but does not let me drop anywhere on the screen even on the original position. – xnoe Dec 20 '15 at 16:49
  • By "anywhere on the screen" do you mean outside of the web page? I'll post a fiddle. – Andrue Anderson Dec 20 '15 at 16:50
  • No I mean on the same page with nothing else on the screen. It shows the circle with the diagonal line symbol and when I release the mouse button it does nothing. – xnoe Dec 20 '15 at 16:53
  • Fixed forgot to remove the draggable: true attribute – xnoe Dec 20 '15 at 16:55
  • Try not to mislead people in your comments. I considered it a problem with my code, and you weren't even trying the fiddle. Thanks. – Andrue Anderson Dec 20 '15 at 16:55
  • Still doesn't work doesn't move anywhere. Suspecting it has something to do with the contained 'div' element inside of the 'aside' element – xnoe Dec 20 '15 at 16:56
  • Unless you want someone to write your code for you, I think I've done all that I can do for you. – Andrue Anderson Dec 20 '15 at 16:58
  • I understand what you mean. – xnoe Dec 20 '15 at 17:00
  • I tried the fiddle. It was a bit laggy for me. I also tried using the code outside the fiddle, and both caused the object I was dragging to lag behind the cursor. Jquery has worked better for me, as it doesn't lag, and if there is lag, it's not noticeable. See my answer if you want the code :P – Cannicide Nov 30 '17 at 23:34
0

Don't know if you want to use Jquery or not, but I know it's way simpler than the raw javascript solution. I've been searching the internet and Stack Overflow for a simple drag-and-drop anywhere on a web page solution, but I couldn't find one that worked. The other answers to this question have been weird for me. They sometimes work, sometimes don't. However, a friend suggested using the Jquery version, and wow, it's so much simpler. Just one line of code!

var dragme = document.getElementsByClassName("dragme");
  for (var i = 0; i < dragme.length; i++) {
   $(dragme[i]).draggable();
  }
.dragme {
  cursor: move;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<h1 class="dragme">Try dragging me anywhere on this sample!</h1>
<h2 class="dragme">Drag me, too!</h2>

IMPORTANT NOTE: This code does not work with just Jquery, it also requires the Jquery UI file.

If the above sample does not work for you, please comment the error it show below. Note that this works for an unlimited amount of elements. This specific example is for elements with the class "dragme", so just replace that with your own class. For one element, it is a single line of code: $("#yourid").draggable();.

This code works for me on Google Chrome. Hope this helps anyone coming on to this page late, like me!

Cannicide
  • 4,360
  • 3
  • 22
  • 42
  • If you don't understand how to use this code, simply search up `.draggable();` in the Jquery UI website. Thanks! – Cannicide Nov 30 '17 at 23:23
  • Isn’t [jquery ui](https://github.com/jquery/jquery-ui) somewhat dated now? I’m a bit worried about pulling in stale code… – Jens Aug 18 '19 at 04:20
  • If you don't want to use jquery ui, there are many newer plugin-based alternatives such as those seen in the answer to the question seen [here](https://stackoverflow.com/questions/4923272/jquery-plugin-alternatives-to-jquery-ui-draggable). The main point was just that this type of mass drag-and-drop functionality is possible and made easier by jquery. I personally like jquery UI despite its age, and it still functions fine and has other functions that I also like using, so it was easier for me to use than getting several plugins. Jquery really simplifies things. – Cannicide Aug 18 '19 at 18:49
  • A simple search for jquery drag-and-drop plugins should be able to get you some good alternatives that work fairly similar to this answer, and have great documentation on their webpages. Simply replace the jquery UI `.draggable()` method with the alternatives' method within the for loop in my code, and it should function perfectly (this may not be the case if the plugin works in a different way). – Cannicide Aug 18 '19 at 18:56