How can I set a "delimiter" area for my javascript draggable object?

Asked

Viewed 63 times

0

Hello! I have an object with drag function in adobe edge Animate and would like to know how it is possible to define an area where it is possible to drag the object. grateful

  • I don’t know Adobe Edge Animate, it works like DOM? with Document/body and interprets Javascript?

  • hello! does not work with Document/body... it is very similar to the script action used in adobe flash but with Javascript... I already have the following code which is a jquery script to drag the object: yepnope({Nope:['jquery-ui-1.10.4.custom.min.js'], complete: init}); Function init(){ Sym. $('car'). draggable(); Sym. $('car2'). draggable(); } how do I limit the drag area? thank you very much!

1 answer

0


If you modify the drag function: Before you update the position of the "draggable" object you just need to check if:

    1. X will be greater than or equal to X of X of the area;
    1. the X + width will be smaller than the X of the area + the width of the area;
    1. the Y will be greater than or equal to the Y of the area;
    1. the Y + height will be less than the Y of the area + the height of the area;

If not, the position of the object is not updated. Where you update the position of the object when dragged you do something like this:

if(obj.x >= limit.x && obj.x + obj.width <= limit.x + limit.width
obj.y >= limit.y && obj.y <= limit.y + obj.height ) {
    // Deve ser provavelmente assim que você atualiza a posição?
    obj.x = e.clientX;
    obj.y = e.clientY;
}
// `limit` seria algo como {x: 10, y: 10, width: 200, height: 200}
// O limit pode ser declarado de forma que possa ser usado.
// var limit = {x: 10, y: 10, width: 200, height: 200};

But it seems that you are using jQuery and the drag function (in your case) is inside. I don’t know jQuery very well. Maybe trying to update the Y of the object every time the mouse pointer moves can be functional if the Y is updated after the object is dragged.

sym.$('car2').mousemove(function() {
    $(this).css({'top': y});
});
  • Hello! I’m new to js, I couldn’t quite understand where I need to put the values... the code I have so far and: yepnope({Nope:['jquery-ui-1.10.4.custom.min.js'], complete: init}); Function init(){ Sym. $('car'). draggable(); Sym. $('car2'). draggable(); } is the idea and make the 2 "car" objects move on the x axis and not on the y axis - thank you!

  • @Miam There is no way to do this if you are not doing the drag internally, or there is not be that you update the Y of the draggable object while the application runs.

  • Klaider, that code you sent me seems compatible! only that I could not understand where I need to change/insert the values of my document area and the area to limit the object...

  • @Why even you can not limit ! It seems that you are using a library. What you can do in this case is to update the Y of the object every time it is dragged with the 'Mousemove' event (the mouse pointer moves), for example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.