doujingxi3356 2012-04-04 11:41
浏览 33

dragdrop保存定位问题

I am currently using a dragdrop script and saving the left and top coordinates of the (position: relative) div(s). My problem is the co-ordinates at which they are saved are not always the same position (as placed) when I reload.

The divs are within a container with a given height and width (this container restrains the divs to only move inside it - perimeter div)

So I am trying to get the co-ordinates to only work/space within the DIV and not the entire page.

I'm using PHP to get the correct divs/widgets for that page which gives the widget class (background and other css based variables)

All widgets have a custom ID in case there is more than one of a particular widget

This is my main code:

<?
    $load = mysql_query("SELECT * FROM `widgets` WHERE `page`='1' AND `show`='yes'")or die(mysql_error());
    while($widget = mysql_fetch_array($load)){
?>
<div id="<? echo $widget['id']; ?>" style="left: <? echo $widget['left']; ?>px; top: <? echo $widget['top']; ?>px;" class="<? echo $widget['widgetid']; ?> draggable ui-widget-content" onmouseup="find('<? echo $widget['id']; ?>','1')"> </div>
<? } ?>

This is my Javascript for saving:

function find(id, page){
    var position = $("#"+id).position();
    $.ajax({
        type: "POST",
        url: "save.php",
        data: "l=" + position.left + "&t=" + position.top + "&request=" + page + "&w=" +id,
        success: function(msg){
            var element = document.getElementById(id);
            element.style.opacity = "1.0";
        }
    });
}

l is left, t is top, both sent onmouseup (w = widgetid, request = pageid)

The variables are saving to the database just not being placed on the page correctly (either moving far to the right or up slightly)

Any help would be greatly appreciated!

Thank you in advance

  • 写回答

1条回答 默认 最新

  • dppcyt6157 2013-02-01 22:34
    关注

    As stated in the jQuery docs on .position():

    Get the current coordinates of the first element in the set of matched elements, relative to the offset parent.

    If you keep reading, it says

    Contrast this with .offset(), which retrieves the current position relative to the document.

    评论

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug