Tariqul Islam Tariqul Islam - 3 years ago 171
jQuery Question

Move 'p' into 'div' when clicked. - animation isn't working

This is just a demo of my real code, but the problem is the same. Is there any other or better solution to this. Why the animate() method isn't working....



$('document').ready(function() {
$('p').click(function() {
var x = $('div').offset();
$(this).animate({
top: $(this).offset({
top: x.top,
left: x.left
})
}, 1000);
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<p style='position:relative;'>Move This</p>

<div style='height:100px;width:100px;border:1px solid black;margin-top:200px;'></div>




Answer Source

$('document').ready(function() {
  $('p').click(function() {
    var x = $('div').offset();
    $(this).animate({
      top: x.top,
      left: x.left
    }, 1000);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<p style='position:relative;'>Move This</p>

<div style='height:100px;width:100px;border:1px solid black;margin-top:200px;'></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download