jQuery(document).ready(function($) {
// Lắng nghe sự kiện thêm vào giỏ hàng thành công bằng AJAX
$('body').on('added_to_cart', function(event, fragments, cart_hash, $button) {
// Tìm icon giỏ hàng trên header của Flatsome
var cart = $('.cart-icon').last();
// Tìm hình ảnh của sản phẩm vừa click
var imgtodrag = $button.closest('.product').find('img').first();
// Nếu ở trang chi tiết sản phẩm (Single Product)
if (imgtodrag.length === 0) {
imgtodrag = $('.woocommerce-product-gallery__image img').first();
}// Tạo hiệu ứng bay
if (imgtodrag.length && cart.length) {
var imgclone = imgtodrag.clone()
.offset({
top: imgtodrag.offset().top,
left: imgtodrag.offset().left
})
.css({
'opacity': '0.9',
'position': 'absolute',
'height': '150px',
'width': '150px',
'z-index': '999999',
'border-radius': '10px',
'box-shadow': '0px 5px 15px rgba(0,0,0,0.2)'
})
.appendTo($('body'))
.animate({
'top': cart.offset().top,
'left': cart.offset().left,
'width': 30,
'height': 30
}, 800); // 800ms là thời gian bay// Ẩn và xóa hình ảnh sau khi bay tới nơi
imgclone.animate({
'width': 0,
'height': 0
}, function() {
$(this).detach();
});
}
});
});