I am attempting to implement a coin collection animation into the cart using only JavaScript and Vue.js. I decided to use raw JavaScript for this purpose after initially following a tutorial on W3schools at https://www.w3schools.com/js/tryit.asp?filename=tryjs_dom_animate_3
Somewhere in my code, there is a destination div representing the cart as shown below:
<div class="coin" id="coin" @click="collect()">
And elsewhere, I have another div that needs to be animated towards the "coin" div:
<div id="animate">
</div>
Here is the CSS styling used:
.coin {
background-image:
url("https://i.pinimg.com/originals/15/ce/a6/15cea65c1fadcfcb144f3b41e32bd9b3.png");
background-size: 100% 100%;
border-radius: 100%;
height: 80px;
position: relative;
width: 80px;
-webkit-transition: 2s linear;
-webkit-transform-style: preserve-3d;
}
#animate {
width: 50px;
height: 50px;
position: absolute;
background-color: red;
}
Below is the JavaScript function being utilized:
collect(){
var elem = document.getElementById("animate");
var pos = 0;
var id = setInterval(frame, 5);
function frame() {
if (pos == 350) {
clearInterval(id);
} else {
pos++;
var testDiv = document.getElementById("coin");
elem.style.top = testDiv.offsetTop;
elem.style.left = testDiv.offsetLeft;
}
}
}
I encountered issues with the animation not working properly as intended according to the left-right properties of the destination div. Any assistance in resolving this issue would be greatly appreciated.