我点击时,我想要移动到其他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.
position:fixed.visibility:hidden或opacity:0/**
* 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)