简体   繁体   中英

Get element position based on size using jquery

I have a draggable div and a droppale div . I get the position of draggable div within the droppable using the following code:

    $(document).ready(function() {
        $('.ff').draggable({
            appendTo: "#pages",
            revert: "invalid"
        });
        $('#pages').droppable({
            accept: ".ff",
            drop: function (event, ui) {                    
                var x = ui.offset.left - $(this).offset().left;
                var y = ui.offset.top - $(this).offset().top;
            }
        });
    });

I need to save these coordinates, but saving the "relative position" I mean, when somebody loads the points (x,y) saved previusly but with a different screen resolution, different device, etc. the points must be in the same position.

So, how to get this "relative position"?

I solved my problem with the following formula:

var x = (dropped.offset.left - container.offset().left) / container.width

This ( x ) is the result that I save. Now, to recreate:

var left = (x * container.width) + container.offset().left;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM