【问题标题】:Add a timer to close Ajax Quick Cart (Shopify) after product is added to cart?添加一个计时器以在产品添加到购物车后关闭 Ajax 快速购物车(Shopify)?
【发布时间】:2015-04-17 10:46:10
【问题描述】:

我已经使用 Timber framework 成功设置了 Shopify Ajax 购物车,所以目前如果您点击购物车外的任何位置(或使用关闭按钮),我的购物车就会关闭。

我希望能够创建一个计时器,以便在将产品添加到购物车并且 ajax 快速购物车打开它然后在 5 秒后关闭,以便用户可以继续购物。如果他们想更长时间地查看购物车,可以点击购物车按钮重新打开,然后单击链接到完整的购物车页面。

有人建议我可以用 wood.RightDrawer.close(); 关闭购物车。我应该在像下面这样的监听器中设置它,但是对于 JS 新手,我正在努力从哪里开始。

jQuery('body').on('ajaxCart.afterCartLoad', function(evt, cart) {
    // start timer function

    // when timer is triggered:
    timber.rightDrawer.close();
});

我尝试了以下方法,但我真的只是在猜测......

jQuery('body').on('ajaxCart.afterCartLoad', function(evt, cart) {
    // start timer function
    $('#AddToCart').on('click', function(){
        setTimeout(function(){
        // when timer is triggered:
        timber.rightDrawer.close();
        },500);
    });
});

我当前的一些代码如下,请参阅与我相同设置的 Timber 框架:

theme.liquid JS:

{% comment %}
Ajaxify your cart with this plugin.
Documentation:
- http://shopify.com/timber#ajax-cart
{% endcomment %}
{% if settings.ajax_cart_enable %}
  {{ 'handlebars.min.js' | asset_url | script_tag }}
  {% include 'ajax-cart-template' %}
  {{ 'ajax-cart.js' | asset_url | script_tag }}
  <script>
    jQuery(function($) {
      ajaxCart.init({
       formSelector: '#AddToCartForm',
       cartContainer: '#CartContainer',
       addToCartSelector: '#AddToCart',
       cartCountSelector: '#CartCount',
       cartCostSelector: '#CartCost',
       moneyFormat: {{ shop.money_format | json }}
     });
   });

jQuery('body').on('ajaxCart.afterCartLoad', function(evt, cart) {
  // Bind to 'ajaxCart.afterCartLoad' to run any javascript after the cart has loaded in the DOM

  timber.RightDrawer.open();
});
</script>
{% endif %}

timber.js.liquid:

timber.drawersInit = function () {
   timber.LeftDrawer = new timber.Drawers('NavDrawer', 'left');
   timber.RightDrawer = new timber.Drawers('CartDrawer', 'right',
   'onDrawerOpen': ajaxCart.load
   });
};

timber.Drawers = (function () {
  var Drawer = function (id, position, options) {
  var defaults = {
  close: '.js-drawer-close',
  open: '.js-drawer-open-' + position,
  openClass: 'js-drawer-open',
  dirOpenClass: 'js-drawer-open-' + position
};

this.$nodes = {
  parent: $('body, html'),
  page: $('#PageContainer'),
  moved: $('.is-moved-by-drawer')
};

this.config = $.extend(defaults, options);
this.position = position;

this.$drawer = $('#' + id);

if (!this.$drawer.length) {
  return false;
}

this.drawerIsOpen = false;
this.init();
  };

 Drawer.prototype.init = function () {
$(this.config.open).on('click', $.proxy(this.open, this));
this.$drawer.find(this.config.close).on('click', $.proxy(this.close, this));
};

  Drawer.prototype.open = function (evt) {
// Keep track if drawer was opened from a click, or called by another function
var externalCall = false;

// Prevent following href if link is clicked
if (evt) {
  evt.preventDefault();
} else {
  externalCall = true;
}

// Without this, the drawer opens, the click event bubbles up to $nodes.page
// which closes the drawer.
if (evt && evt.stopPropagation) {
  evt.stopPropagation();
  // save the source of the click, we'll focus to this on close
  this.$activeSource = $(evt.currentTarget);
}

if (this.drawerIsOpen && !externalCall) {
  return this.close();
}

// Add is-transitioning class to moved elements on open so drawer can have
// transition for close animation
this.$nodes.moved.addClass('is-transitioning');
this.$drawer.prepareTransition();

this.$nodes.parent.addClass(this.config.openClass + ' ' + this.config.dirOpenClass);
this.drawerIsOpen = true;

// Run function when draw opens if set
if (this.config.onDrawerOpen && typeof(this.config.onDrawerOpen) == 'function') {
  if (!externalCall) {
    this.config.onDrawerOpen();
  }
}

if (this.$activeSource && this.$activeSource.attr('aria-expanded')) {
  this.$activeSource.attr('aria-expanded', 'true');
}

// Lock scrolling on mobile
this.$nodes.page.on('touchmove.drawer', function () {
  return false;
});

this.$nodes.page.on('click.drawer', $.proxy(function () {
  this.close();
  return false;
}, this));
  };

  Drawer.prototype.close = function () {
if (!this.drawerIsOpen) { // don't close a closed drawer
  return;
}

// deselect any focused form elements
$(document.activeElement).trigger('blur');

// Ensure closing transition is applied to moved elements, like the nav
this.$nodes.moved.prepareTransition({ disableExisting: true });
this.$drawer.prepareTransition({ disableExisting: true });

this.$nodes.parent.removeClass(this.config.dirOpenClass + ' ' + this.config.openClass);

this.drawerIsOpen = false;

this.$nodes.page.off('.drawer');
  };

  return Drawer;
})();

// Initialize Timber's JS on docready
$(timber.init);

【问题讨论】:

  • 我已经更新了我的问题,我已经尝试过但目前没有工作,我是否朝着正确的方向前进?
  • 你的 ajaxCart.afterCartLoad 监听器被调用了吗?您的代码看起来不错,因此请调试代码(使用 Safari 或 Chrome 中的开发人员工具或 Firefox 中的 Firebug),或者在侦听器中添加一个警报,以确保它按您预期的方式被调用。

标签: javascript jquery ajax


【解决方案1】:

使用setinterval设置时间为5s

     jQuery('body').on('click','#AddToCart', function(evt, cart) {

  timber.RightDrawer.open();

       var myVar = setInterval(function(){ 
        timber.RightDrawer.close();
        clearInterval(myVar);
        }, 5000);
});

【讨论】:

  • setTimeout 不会比 setInterval 更受欢迎,因为它只运行一次,而且您不必添加额外的代码来清除间隔?
  • 我之前尝试过类似的方法,但无法正常工作。也刚刚尝试了您的代码 sn-p ,但这似乎也不起作用。请问是不是我放错地方了?我目前将它放在我的 theme.liquid 模板中的结束脚本标记之前。
  • 这很好用,只是我只需要在单击#AddtoCart 按钮时应用它。我是否可以将它包装在一个函数中,就像我上面原始问题中的示例一样?
  • 我尝试了两个选项,第二个打破了它,第一个工作一次,然后不再工作。知道为什么会这样吗?
  • 因为事件更新了第二部分代码
猜你喜欢
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
  • 2013-05-07
相关资源
最近更新 更多