jQuery的拖动n下降恢复到原来的位置不起作用 [英] jquery drag n drop revert back to original position not working

查看:105
本文介绍了jQuery的拖动n下降恢复到原来的位置不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在列出可以从左侧容器拖动到放置区域的产品列表.产品被拖动到放置容器上,但是此后,我们单击(X)关闭按钮,然后将其恢复到其原始位置.直到拖动产品并将其放入容器中的零件都可以正常工作.但是在那之后我得到了这个错误

I am making a list of products which can be dragged from left container to the drop area . The products are being dragged onto the drop container but after that a (X) close button gets active on which we click then the product is reverted back to its original position . The part till dragging the product and dropping it into the container works fine . but after that i get this error

Uncaught TypeError: Cannot read property 'originalPosition' of undefined 

我的代码(jquery 1.9版和jquery ui 1.10.3版)

my code ( jquery version 1.9 and jquery ui version 1.10.3 )

     $(document).ready(function () {
$('.drg-bx1').droppable({
    tolerance: 'fit'
});

$('.mt-drg-ob').draggable({
    revert: 'invalid',
    stop: function(){
        $(this).draggable('option','revert','invalid');
        $(this).find('.undo').show();
    }
});

$('.mt-drg-ob').find('.undo').click(function(i, e) {
    var $div = $(this).parent();
    revertDraggable($div);
});

$('.drg-bx1').droppable({
    greedy: true,
    tolerance: 'touch',
    drop: function(event,ui){
       // ui.draggable.draggable('option','revert',true);
        if (!ui.draggable.data("originalPosition")) {
            ui.draggable.data("originalPosition",
                              ui.draggable.data("draggable").originalPosition);

        }


        $(this).find('.undo').show();
    }
});


});
function revertDraggable($selector) {
    $selector.each(function() {
        var $this = $(this),
            position = $this.data("originalPosition");

        if (position) {
            $this.animate({
                left: position.left,
                top: position.top
            }, 500, function() {
                $this.data("originalPosition", null);
            });
        }
    });

     $selector.find('.undo').hide();

}

推荐答案

使用ui.draggable.data("uiDraggable").originalPosition代替

ui.draggable.data("draggable").originalPosition);

完整在这里回答

这篇关于jQuery的拖动n下降恢复到原来的位置不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆