Tween <div> to another <div>
I am trying to learn jQuery and am doing a small project to try to learn how to practice, but I am fixated on how to animate the movement of one div into another div.
To clarify, when I click on one div, I would like the other div to move to the div I clicked on. I use appendTo () to move it there, but as you already know, the movement is instantaneous, and I would like to animate the movement.
Here is a scenario of what is currently happening: https://jsfiddle.net/Chirpizard/jyatn89r/
Just click on the blue dot to see what happens.
Here is the 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");
}
});
});
, . , , !
+4
3
... , appendTo..
$(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");
}
});
});
+2
appendTo() ?
JS:
$(document).ready(function () {
$(".node").click(function() {
if ($(this).hasClass('unlocked')) {
$("#main-char").animate( {
top: $(this).offset().top -25
}, 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");
}
});
});
0
$(document).ready(function () {
$(".node").click(function() {
var nodePosition = $(this).position();
$("#main-char").animate( {
top: nodePosition.top + 30
}, 1000);
});
});#world-map {
background: #222;
min-height: 1500px;
position: relative;
padding: 30px 0 0 0;
}
#main-char {
width: 30px;
height: 30px;
border-radius: 100%;
background: red;
margin: 0 auto 0 auto;
position: absolute;
top: 0;
left: 50%;
margin-left: -15px;
}
.node {
width: 50px;
height: 50px;
border-radius: 100%;
background: blue;
box-sizing: padding-box;
margin: 20px auto 0 auto;
display: block;
}
.node1 {
top: 100px;
}
.node2 {
top: 400px;
}
.node3 {
top: 700px;
}
.node4 {
top: 1000px;
}
.node5 {
top: 1300px;
}
.activeNode {
background: #aaa;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="world-map">
<div id="main-char"></div>
<div class="node node1 unlocked"></div>
<div class="node node2 unlocked"></div>
<div class="node node3 unlocked"></div>
<div class="node node4 unlocked"></div>
<div class="node node5 unlocked"></div>
</div>0