Back to Snippets

Interactive Drag to Cart Animation JS

Press on the product image and drag it toward the cart. A semi transparent copy follows your cursor, and dropping it on the cart bumps the counter.

The copy is a real img element created on pointerdown and positioned with fixed coordinates, so it can travel anywhere on the page without being clipped by a parent. The original fades while the drag runs, and the source image disables the browser's native image dragging so it never fights the script.

Dropping is a bounding box test. On pointerup the drop point is compared against the cart's getBoundingClientRect, and only a hit inside those bounds runs the shrink and fly in animation. A miss removes the copy and leaves the count alone. Everything uses pointer events rather than mouse events, so touch works without a second code path.

430
#javascript
#css
#drag and drop
#cart
#ecommerce
#interaction
#animation
#shopping
#ui