将 <div> 移动到另一个 <div> 的动画

Chi*_*ard 4 html javascript css jquery animation

我正在尝试学习 jQuery,并正在修改一个小项目来尝试通过实践来学习,但我一直专注于如何为一个 div 的移动设置动画到另一个 div。

澄清一下,当我单击一个 div 时,我希望将另一个 div 移动到我单击的 div。我正在使用appendTo()将他移动到那里,但正如你所知,移动是即时的,我想为移动设置动画。

这是当前正在发生的事情的小提琴:https ://jsfiddle.net/Chirpizard/jyatn89r/

只需单击蓝点即可查看发生了什么。

这是JS:

$(document).ready(function () {
  $(".node").click(function() {
    if ($(this).hasClass('unlocked')) {
      $("#main-char").appendTo(this).animate( {
        top: "+=50"
      }, 1000, function() {
      });
      $(".node").removeClass("activeNode"); //Removes all instances of activeNode
      $(this).addClass('activeNode') //Adds activeNode class to the node clicked on
    }
    else {
      console.log("Sorry, broken");
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

我检查了其他几篇文章,但没有找到我正在寻找的确切解决方案。任何有关如何到达红点以缓慢移动到单击的元素的指导将不胜感激!

谢谢

Moh*_*sef 6

如果我明白你的意思...我认为你不需要appendTo..只需使用animate

$(document).ready(function () {

  $(".node").click(function() {

    if ($(this).hasClass('unlocked')) {
      $("#main-char").animate( {
        top: $(this).offset().top -27
      }, 1000, function() {
      });

      $(".node").removeClass("activeNode"); //Removes all instances of activeNode
      $(this).addClass('activeNode') //Adds activeNode class to the node clicked on
    }
    else {
      console.log("Sorry, broken");
    }
  });

});
Run Code Online (Sandbox Code Playgroud)

演示版