【发布时间】:2014-08-05 18:16:03
【问题描述】:
我有以下代码为 Opencart 实现飞到购物车。
问题:第一次点击 AddtoCart 时,没有产品飞到购物车 - 但产品已成功添加。第二次单击 AddtoCart 时,产品会飞到购物车(这就是我想要的)。第三次单击 AddtoCart 时,两个图像克隆飞到购物车,依此类推。
谁能指出我下面代码中的错误。
function addToCart(product_id, quantity) {
quantity = typeof(quantity) != 'undefined' ? quantity : 1;
i = 10;
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: 'product_id=' + product_id + '&quantity=' + quantity,
dataType: 'json',
success: function(json) {
//alert(this);
$('.success, .warning, .attention, .information, .error').remove();
if (json['redirect']) {
location = json['redirect'];
}
//alert(this);
//alert("button is clicked");
if (json['success']) {
alert(this);
//alert("button is clicked");
$('.button').click(function () {
// alert("button is clicked");
adt = $(this);
var cart = $('#cart');
// alert (this);
var imgtodrag = $(adt).parent().siblings(".image").find("img").eq(0);
//alert (this);
// alert(imgtodrag);
if (imgtodrag) {
//alert(imgtodrag);
i = i + 30;
var imgclone = imgtodrag.clone()
.offset({
top: imgtodrag.offset().top,
left: imgtodrag.offset().left
})
.css({
'opacity': '0.5',
'position': 'absolute',
'height': '150px',
'width': '150px',
'z-index': '100'
})
.appendTo($('body'))
.animate({
'top': cart.offset().top + 10,
'left': cart.offset().left + i ,
'width': 75,
'height': 75
}, 1000, 'easeInOutExpo');
/*setTimeout(function () {
cart.effect("shake", {
times: 2
}, 200);
}, 1500); */
imgclone.animate({
'width': 15,
'height': 15
}, function () {
$(this).detach()
});
}
});
// $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
$('.success').fadeIn('slow');
$('#cart-total').html(json['total']);
$('html, body').animate({ scrollTop: 0 }, 'slow');
}
}
});
}
【问题讨论】:
标签: javascript php jquery opencart shopping-cart