jQuery UI中可拖动的先前位置?

时间:2013-11-17 06:47:43

标签: jquery jquery-ui draggable

JQuery:

 $('.ClassName').draggable( {

       start: function() {
                              //other Code
                          },
        stop: function(event, ui) {

             var oldPos = ($(this).data("draggable").originalPosition.left); //Got Error
             var newPos = ui.position.left; //I got new Postion here

                            }
    });

当我使用此代码时,我在控制台中出错:

TypeError: $(...).data(...) is undefined

有没有办法在draggable中找到oldPos或我在代码中遗漏了什么?感谢。

2 个答案:

答案 0 :(得分:0)

这是一个工作样本: http://jsfiddle.net/sTD8y/27/

$(function() {
$("#draggable").draggable({ 
    revert:  function(dropped) {
         var $draggable = $(this),
             hasBeenDroppedBefore = $draggable.data('hasBeenDropped'),
             wasJustDropped = dropped && dropped[0].id == "droppable";
         if(wasJustDropped) {
             // don't revert, it's in the droppable
             return false;
         } else {
             if (hasBeenDroppedBefore) {
                 // don't rely on the built in revert, do it yourself
                 $draggable.animate({ top: 0, left: 0 }, 'slow');
                 return false;
             } else {
                 // just let the build in work, although really, you could animate to 0,0 here as well
                 return true;
             }
         }
    }
});

$("#droppable").droppable({
    activeClass: 'ui-state-hover',
    hoverClass: 'ui-state-active',
    drop: function(event, ui) {
        $(this).addClass('ui-state-highlight').find('p').html('Dropped!');
        $(ui.draggable).data('hasBeenDropped', true);
    }
});

});

答案 1 :(得分:0)

在js1.4版本中

var oldPos = ($(this).data("draggable").originalPosition.left); 

它的工作。

当我使用js1.9时,它给我一个麻烦。我认为这是一个dom旅行问题。我解决了这个问题:

 var oldPos = ui.originalPosition.left;