-2

I am trying to do the flying cart which means when ever Add to cart button is clicked, the product image should be taken as clone and fly into the basket. The same as this video

you tube video

The video has the code as

$("button").on("click",function(){
$(this).closest("li")
.find("img")
.clone()
.addClass("zoom")
.appendTo("body");

setTimeout(function(){
$(".zoom").remove();
},1000);
});

The same I should do in javascript / html without Jquery.

My trying

  var itm1 = document.getElementById("one");
  // HEre I can take the clone of the image.
  var cln1 = itm1.cloneNode(true);
  // Here I need to add the css to this image as per the video.
Muthukumar Marichamy
  • 1,175
  • 1
  • 17
  • 39

2 Answers2

1

You just have find the element, copy it, add it to the document and then add the class that has the animation.

Here is a working example:

document.querySelectorAll(".item").forEach(function(item) {
  item.querySelector(".add").addEventListener("click", function() {
    document.body.appendChild(item.querySelector(".image").cloneNode()).classList.add("floating");
  });
});
.image {
  height: 40px;
  width: 40px;
}

.image.floating {
  animation: fade-out-in-right 2s linear 0s 1 normal forwards;
  position: absolute;
  right: 20px;
  top: 20px;
}

@keyframes fade-out-in-right {
  0% {
    transform: translateX(-100px);
    opacity: 0;
  }
  80% {
    transform: translateX(-20px);
    opacity: 1;
  }
  100% {
    transform: translateX(0px);
    opacity: 0;
  }
}
<div class="item">
  <div class="image" style="background: blue"></div>
  <button class="add">Add To Cart</button>
</div>
<div class="item">
  <div class="image" style="background: red"></div>
  <button class="add">Add To Cart</button>
</div>
nick zoum
  • 7,216
  • 7
  • 36
  • 80
  • 1
    I applied this to see the effect in the example [in codepen]( https://codepen.io/iganchev87/pen/bXxRer). I think that gives a solution to the problem. – Ivan Ganchev Aug 09 '19 at 15:05
0
cln1.classList.add("zoom");
var body = document.getElementsByTagName("BODY")[0];
body.innerHTML = cln1;

How to add content to html body using JS?

You should read up on modifying the DOM.

I haven't tested this code but in theory I believe it will work.

elke_wtf
  • 887
  • 1
  • 14
  • 30