简体   繁体   English

如何延迟计数器直到动画结束

[英]How to delay a counter until animation is finished

I have a fancy animation for a shoppingcart which moves a cart icon from the product to the top corner and it will do a +1 on the counter with angularJS. 我有一个精美的购物车动画,它将购物车图标从产品移到顶部,它将使用angularJS在柜台上执行+1。 However at the time the animated cart reaches the top the counter is already +1. 但是,当动画购物车到达顶部时,计数器已经为+1。 It would be awesome if I was able to delay the angularJS counter so that it will be added when the animation is completed. 如果我能够延迟angularJS计数器,以便在动画完成时添加它,那将是非常棒的。

HTML HTML

<li>({{orderTotal()}}) <i id="cart" class="fa fa-shopping-cart"></i> Order </a></li>

JS JS

function flyToElement(flyer, flyingTo) {
  if (jQuery(window).width() > 767) {

    var $func = $(this);
    var divider = 1;
    var flyerClone = $(flyer).clone();
    $(flyerClone).css({position: 'absolute', top: $(flyer).offset().top + "px", left: $(flyer).offset().left + "px", opacity: 1, 'z-index': 1000});
    $('body').append($(flyerClone));

    var gotoX = $(flyingTo).offset().left + ($(flyingTo).width() /2) - ($(flyer).width()/divider)/2-5;
    var gotoY = $(flyingTo).offset().top + ($(flyingTo).height() /2) - ($(flyer).height()/divider)/2-5;

    $(flyerClone).animate({
        opacity: 1.0,
        left: gotoX,
        top: gotoY

    }, 500,
    function () {
      $(flyingTo).fadeOut('fast', function () {
        $(flyingTo).fadeIn('fast', function () {
          $(flyerClone).fadeOut('fast', function () {
            $(flyerClone).fadeIn('fast', function () {
              $(flyerClone).fadeOut('fast', function () {
                $(flyerClone).remove();
              });
            });
          });
        });
      });
    });
  };
}

AngularJS AngularJS

$scope.orderTotal = function(index) {
  var orderTotal = 0;
  angular.forEach($localStorage.items, function(items) {
    orderTotal += items.quantity;
  })
  return orderTotal;
};

I'm looking for some advise on how to do this. 我正在寻找有关如何执行此操作的建议。 Thanks 谢谢

SOLUTION

Using $timeout() 使用$ timeout()

var kvv = angular.module('kvv', ['ngStorage']);
kvv.controller('CartController', function($scope, $localStorage, $sessionStorage) {


    $scope.addToCart = function(index, title, desc, price, timeout) {
        var found = false;
        angular.forEach($localStorage.items, function(items) {
            if (items.id  === index) {
              $timeout(function() {(items.quantity++)}, 500);
              found = true;
            }
        });
        if (!found) {
           $timeout(function() {$localStorage.items
           .push(angular.extend({
             id: index,
             title: title,
             quantity: 1,
             price: price}, index))},500);
        }
      };
});

In angular your data is bound at the moment the event is executed. 在执行事件的那一刻,您的数据就被绑定了。 And anything bound to that data will show the current state of the data. 与该数据绑定的所有内容都将显示该数据的当前状态。 You should either move your jQuery or jqlite into some form of service in Angular and use a $apply at the end of our function nesting or chaining or wrap it in the $watch as previously mentioned. 您应该将jQuery或jqlite移到Angular中的某种形式的服务中,并在函数嵌套或链接的末尾使用$ apply或将其包装在$ watch中,如前所述。 Good info on $apply and $watch here . 有关$ apply和$ watch的好信息,请点击此处 Or Just do the entire thing in jQuery using the same strategies. 或使用相同的策略在jQuery中完成整个操作。 The issue comes down to using two DOM tools separately. 问题归结于分别使用两个DOM工具。 You can totally use them together. 您可以完全一起使用它们。

You should use css for the animation. 您应该将css用于动画。 Using JQuery in AngularJS should be minimized as much as possible, or you're complicating your life needlessly. 应尽可能减少在AngularJS中使用JQuery,否则将不必要地使您的生活复杂化。 Future maintenance will be a nightmare. 将来的维护将是一场噩梦。

You can accomplish this using an animation directive that adds and removes a certain class based on the animation state, and combine that with a $watch to determine when the final state of the animation is executed (ie. element is removed, or final class [ie. animation-end] is attached). 您可以使用动画指令完成此操作,该指令根据动画状态添加和删除某个类,然后将其与$ watch结合使用,以确定何时执行动画的最终状态(即元素被删除,或最终类[即动画末端]。

Maybe $timeout is what you need. 也许$ timeout是您所需要的。

var kvv = angular.module('kvv', ['ngStorage'])
  .controller('CartController', function($scope, $timeout, $localStorage, $sessionStorage) {
    $scope.checked = false;
    $scope.addToCart = function(index, title, desc, price) {
      $scope.checked = true;
      var found = false;
      angular.forEach($localStorage.items, function(items) {
        if (items.id  === index) {
          $timeout(function() {
            (items.quantity++);
            $scope.checked = false;
          }, 500);
          found = true;
        }
      });
      if (!found) {
       $timeout(function() {
         $localStorage.items.push(angular.extend({
           id: index,
           title: title,
           quantity: 1,
           price: price}, index))
       },500);
      }
    };
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM