jquery - 单击时将 'p' 移至 'div'。 - 动画不起作用

标签 jquery jquery-animate

这只是我真实代码的演示,但问题是一样的。有没有其他或更好的解决方案。为什么 animate() 方法不起作用......

$('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>

最佳答案

top: $(this).offset({ top: x.top, left: x.let }) 是一个不正确的构造,而且拼写错误。

这是包括克隆在内的更正。请注意,我必须将 div 位置设置为绝对位置,并在动画之后将 p 从相对位置更改为静态位置,以将 p 插入到 div 中

$('document').ready(function() {
  $('p').click(function() {
    var $p = $(this),
      $div = $('div'),
      $pc = $p.clone(),
      x = $div.offset();
    $pc.insertAfter($p).animate({
      top: x.top,
      left: x.left
    }, 1000,function() {
      $div.append($pc.css({"position":"static"}))
    });
  });
});
.target {position:absolute; top:100px;height:100px;width:100px;border:1px solid black;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<p style='position:relative;'>Move This</p>

<div class="target"></div>

关于jquery - 单击时将 'p' 移至 'div'。 - 动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46517109/

相关文章:

javascript - 使用 jquery 动画圆形缩略图大小在鼠标悬停时增长

javascript - 将日期粘在顶部,直到滚动期间出现新日期,就像 WhatsApp 中一样

jquery - 如何添加自定义 html bootstrap 选择器?

javascript - 动态添加列到表的中间

javascript - 如何使用左/上重新定位相对DIV?

jQuery if 语句 - 它可以先执行(还是必须 if 执行在事件之后)?

javascript - Draggable 和 ondragstart 之间的区别

jquery - IE7 问题,一个 div 位于另一个 div 之上,因此它看起来被禁用

javascript - Animate() 无法与scrollTop 一起正常工作

css - 使用 CSS 在导航栏上滑动(动画)bg 图像位置