jquery draggable options

后端 未结 6 1596
梦谈多话
梦谈多话 2021-01-11 15:48

i want the draggable object to revert back to its original position when i click a button. i used \'destroy\' option but it doesnt seem to work. it disables the dragging but

6条回答
  •  无人及你
    2021-01-11 16:18

    function revertable($drag){     
        $drag.data({ top: $drag.css('top'), left: $drag.css('left') })  
        $('.'+$drag.attr('id')+'.revert').data({ revert: '#'+$drag.attr('id') })
            .on('click',function(){
                var $rev = $( $(this).data('revert') );
                $rev.css({ top: $rev.data('top'), left: $rev.data('left') });
        })
    }
    

    // simply call the function in the create event

    var $drag = $('#my_draggable'); //your element
    $drag.draggable({
        create: revertable($drag)
    });
    

    //test it: change the top/left values, refresh, click the button to see it work.

    Drag Me Around

    ...tested, should work for any element (requires jquery 1.7 for .on() function)

提交回复
热议问题