如何在appendTo之后使div飞到另一个div

Jun*_*say 3 javascript jquery

我点击时,我想要移动到其他div的元素.我找到了appendTo,但我不知道如何让元素在转换中飞向另一个div.

<div id="top">
  <button id="b1">B1</button>
</div>
<br>
<br>
<br>
<br>
<div id="bottom">
  <button id="b2">B2</button>
</div>

<script>
$('#b1').click(function() {
  $('#b1').appendTo($('#bottom'));
})

$('#b2').click(function() {
  $('#b2').appendTo($('#top'));
})
</script>
Run Code Online (Sandbox Code Playgroud)

是否有一种简单的方法可以让按钮在点击后"飞行"?现在,我只是让它们淡出并进入新的div.

Rok*_*jan 6

  1. 在当前坐标处创建一个元素的"飞行克隆 " position:fixed.
  2. 将元素附加到目标
  3. 使元素隐藏使用visibility:hiddenopacity:0
  4. 将克隆从起始坐标设置为元素的新坐标
  5. 摧毁克隆
  6. 使元素可见
  7. 如果元素已经在目的地(即:在后续调用中),则阻止飞行

/** 
 * Fly element to destination parent
 * Use like: flyMeTo("#bird", "#destinationParent")
 * @param el {String} Selector (or `this`) of the flying element
 * @param destination {String} Destination parent selector
 * @param prepend {Boolean} Optional. Set to true to use prepend (instead of append)
 */
function flyMeTo(elem, destination, prepend) {

  var $elem = $(elem);
  var $dest = $(destination);
  
  // Early exit - if already in destination
  if($elem.parent().is(destination)) return;
  
  var $klon = $elem.clone().insertAfter($elem);
  var start = elem.getBoundingClientRect();

  $klon.css({position:"fixed", zIndex:9999, left:start.left, top:start.top, pointerEvents:'none'});
  $elem.css({opacity:0})[prepend?'prependTo':'appendTo']( $dest );

  var end = elem.getBoundingClientRect(); // Get new coordinates after append/prepend
  $klon.animate({left:end.left, top:end.top}, 600, function() {
    $klon.remove();         // Remove flying clone once it reaches destination
    $elem.css({opacity:1}); // Show original Element
  });
}


// DEMO:
$('#b1').click(function() {
  flyMeTo( this, '#bottom', true ); // By passing `true` it will prepend!
});
$('#b2').click(function() {
  flyMeTo( this, '#top' );
});
Run Code Online (Sandbox Code Playgroud)
body {
  height: 200vh;
}
Run Code Online (Sandbox Code Playgroud)
<br>
<br>
<br>
<div id="top">
  <button id="b1">B1</button>
</div>
<br>
<br>
<br>
<br>
<div id="bottom">
  <button id="b2">B2</button>
</div>

<script src="//code.jquery.com/jquery-3.1.0.js"></script>
Run Code Online (Sandbox Code Playgroud)