How do I get the coordinate position after using jQuery drag and drop?

前端 未结 9 1241
北荒
北荒 2020-11-29 20:36

How do I get the coordinate position after using jQuery drag and drop? I want to save the coordinate to a database, so that next time I visit, the item will be in that posi

相关标签:
9条回答
  • 2020-11-29 21:01

    I would start with something like this. Then update that to use the position plugin and that should get you where you want to be.

    0 讨论(0)
  • 2020-11-29 21:13

    Cudos accepted answer is great. However, the Draggable module also has a "drag" event that tells you the position while your dragging. So, in addition to the 'start' and 'stop' you could add the following event within your Draggable object:

        // Drag current position of dragged image.
        drag: function(event, ui) {
    
            // Show the current dragged position of image
            var currentPos = $(this).position();
            $("div#xpos").text("CURRENT: \nLeft: " + currentPos.left + "\nTop: " + currentPos.top);
    
        }
    
    0 讨论(0)
  • 2020-11-29 21:17

    I just made something like that (If I understand you correctly).

    I use he function position() include in jQuery 1.3.2.

    Just did a copy paste and a quick tweak... But should give you the idea.

    // Make images draggable.
    $(".item").draggable({
    
        // Find original position of dragged image.
        start: function(event, ui) {
    
            // Show start dragged position of image.
            var Startpos = $(this).position();
            $("div#start").text("START: \nLeft: "+ Startpos.left + "\nTop: " + Startpos.top);
        },
    
        // Find position where image is dropped.
        stop: function(event, ui) {
    
            // Show dropped position.
            var Stoppos = $(this).position();
            $("div#stop").text("STOP: \nLeft: "+ Stoppos.left + "\nTop: " + Stoppos.top);
        }
    });
    
    <div id="container">
        <img id="productid_1" src="images/pic1.jpg" class="item" alt="" title="" />
        <img id="productid_2" src="images/pic2.jpg" class="item" alt="" title="" />
        <img id="productid_3" src="images/pic3.jpg" class="item" alt="" title="" />
    </div>
    
    <div id="start">Waiting for dragging the image get started...</div>
    <div id="stop">Waiting image getting dropped...</div>
    
    0 讨论(0)
提交回复
热议问题