Return Divs to start position

Asked

Viewed 360 times

2

At this time the drag3 always returns the initial position, except when it is placed in the drop1. In the complete game it has 4 drag s and 4 drop s but just 1 of each for example and give to understand how I have this, I think.

What I really want, as I said, is that all drag s go back to the starting position without refreshing the page (which is what I do in this example) when I click the button/image to restart.

$("#drag3").draggable({
  revert: true,
  containment: "#fundo-interno"
});

$("#drop1").droppable({
  drop: function(event, ui) {
    if (ui.draggable.attr('id') == 'drag3') {
      $("#drag3").draggable({
        revert: false,
        containment: "#fundo-interno"
      });
      $(this)
        .addClass("alterar1")
        .find("p")
      alert("Muito Bem !");

    } else {
      $(this)
        .find("p")
      alert("Tenta denovo !");
    }
  }
});
#drag3 {
  float: left;
  margin-left: 75px;
  margin-top: 25px;
  background-color: #727170;
  width: 45px;
  height: 45px;
}
#drop1 {
  float: left;
  margin-left: 20px;
  margin-top: 9px;
  width: 210px;
  height: 180px;
  float: left;
  background-position: center;
  border: 1px solid grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<input type="reset" name="recomecar" value="Ok" id="recomecar" onclick="window.location.reload( true );">
<img src="seta.png" width="100px" height="100px">

  • 4

    Please show the code you already have, and point out what is causing you difficulty. As it stands, the question will certainly be closed as "too broad". Feel free to [Dit] the question with the relevant information.

  • My view is aching from so much effort to read, could organize the question?

2 answers

0

Hold position x and y and then when you need to arrow the values of the left and top hers.

0


If you notice, when it moves div using the draggable jQuery UI, the element automatically receives the position as relative, and then its Top and Left positions are named 0. You can use this in your favor, because automatically the initial position of the element will be when it is in position Top and Left: 0. Therefore, to make the element return to its source location, you can simply send the element to the position Top and Left: 0. For it to come back with an animation, you can use the function animate jQuery. Example:

// "#reset" é o botão para resetar a posição do elemento
$("#reset").click(function() {
    $("#drag3").animate({
        top:"0",
        left:"0"
    }, 1000);
});

See an example working on Jsfiddle: http://jsfiddle.net/ejowy0j5/

Browser other questions tagged

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