1

I'm using jquery UI draggable. I do some works in drag function. for example I scale the dragging element according to it's position. I want to drag elements automatically to certain (x, y) (something like jquery animate({left:x, top:y}, 1000)); but I want to trigger drag function and scale element when is animating. how can I do this?

2 Answers 2

1

I suggest another approach to do that.

Use an external function to do the scale effect, and call it from both events (drag and animate):

var $myDraggable = $('#draggable').draggable({
    drag: function( event, ui ) {
        scale(ui.offset.left, ui.offset.top); 
    }
});

$('button').on('click', function(){
    $myDraggable.animate(
    { left:100, top:100 }, 
    {
        duration: 1000,
        progress: function(draggable){
           scale(draggable.elem.offsetLeft, draggable.elem.offsetTop);
        }
    });
});

function scale(left, top){
    //your scaling logic here
    console.log("scaling", left, top);
}

See this example: FIDDLE

Sign up to request clarification or add additional context in comments.

1 Comment

Nice answer! Progress does the job! Thanks so much :*
0

https://jsfiddle.net/moongod101/8gdvz9jL/

PS:This code offer a button toggle function

$(function(){

  $button = $('button')
  $box = $('.box')
  $click = 0

  $button.click(function(){

    if($click !=0){
        $click ++
        $box.removeClass('active')
    }else{
        $click --
        $box.addClass('active')
    }

  });

});

1 Comment

Thanks. but I want to jquery-ui detects that the element is dragging and calls drag function of draggable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.