【问题标题】:Fly to Cart for Opencart为 Opencart 飞到购物车
【发布时间】: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


    【解决方案1】:

    问题是您仅在将产品添加到购物车后才添加到$('.button').click 事件(至少从上面的代码中)。我会亲自创建一个为该按钮创建该事件的函数。您的代码将变为:

    function bindButtonClick() {
        $('.button').off('click').click(function () {
            adt = $(this);
            var cart = $('#cart');
            var imgtodrag = $(adt).parent().siblings(".image").find("img").eq(0);
    
            if (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');
    
                imgclone.animate({
                    'width': 15,
                    'height': 15
                }, function () {
                    $(this).detach()
                });
            }
        });
    }
    
    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) {
    
                $('.success, .warning, .attention, .information, .error').remove();
    
                if (json['redirect']) {
                    location = json['redirect'];
                }
    
                if (json['success']) {
    
                    bindButtonClick();
    
                    $('.success').fadeIn('slow');
    
                    $('#cart-total').html(json['total']);
    
                    $('html, body').animate({ scrollTop: 0 }, 'slow'); 
                }
            }
        });
    }
    

    现在,函数 bindButtonClick 必须在 domready 上调用

    $(function(){
        bindButtonClick();
    });
    

    另外,请注意 $('.button').off('click') 取消绑定您之前的点击,这样它就不会被多次触发。

    注意事项:
    - 代替alert,尝试学习浏览器的开发工具,使用console.log 代替(在Firefox 中使用Firebug,在Google Chrome 中使用Chrome Developer Tools)。您将不会收到烦人的警报,并且可以在控制台中看到您的所有 javascript 错误。

    【讨论】:

    • 这对我来说几乎是完美的。谢谢。但是,有一个问题 - 第一项不会飞,而是悄悄地添加到购物车中。第二次点击它就完美了。
    • 您是否将$(function(){ bindButtonClick(); }); 放入您的html 中?这就是它第一次“飞”起来的原因。看我的回答!
    • 现在还有一个问题,如果我没有打扰你的话。我想始终显示添加到购物车中的产品。所以我在上面注释掉了 detach() 函数。现在可以根据需要添加产品克隆。但是,按 Enter 后,添加的产品克隆列表会消失。我想保留添加的产品克隆,以便始终显示。
    • 该功能必须用 php 完成。那里必须有一个循环(在视图中)。在该循环内创建另一个具有当前产品数量的循环。这样,在页面刷新或表单提交时,您将获得相同的产品列表。
    猜你喜欢
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多