Angular JS飞行购物车动画指令

时间:2015-03-02 08:59:31

标签: javascript jquery html angularjs angularjs-directive

目前,我正试图创建一个动画“飞行购物车”的角度指令'。

我已经找到了大量的jQuery解决方案,但没有在纯粹的Angular Directive中完成。我想要实现的jQuery飞行器演示就在这里......

原始jQuery Flying Cart Codepen:

http://codepen.io/ElmahdiMahmoud/pen/tEeDn

我对Angular Directives并不熟悉如何完成此任务。我已经开始使用自己的Codepen,希望能够把它弄清楚,但我无法绕过需要发生的事情和方式。

我目前的Codepen:

http://codepen.io/anon/pen/emKoov?editors=101

var myApp = angular.module('flyingCartApp', []);

myApp.directive('addToCartButton', function() {

 function link(scope, element, attributes) {
    element.on('click', function(){
    console.log('i was clicked');
    console.log('Image source', attributes.src)
    console.log('Target element', $(attributes.target))
  });
};

return {

  restrict: 'E',
  link: link,
  transclude: true,
  replace: true,
  scope: {},
  template: '<button class="add-to-cart" ng-transclude></button>'
  };
});

1 个答案:

答案 0 :(得分:8)

这是解决方案 Codepen link

JS:

var a={
    funct:function(){
        // ...
    }
}
a.funct();

function funct() {
    // ...
}
funct();

的CSS:

var myApp = angular.module('flyingCartApp', []);

myApp.directive('addToCartButton', function() {


  function link(scope, element, attributes) {
    element.on('click', function(event){
      var cartElem = angular.element(document.getElementsByClassName("shopping-cart"));
      console.log(cartElem);
      var offsetTopCart = cartElem.prop('offsetTop');
      var offsetLeftCart = cartElem.prop('offsetLeft');
      var widthCart = cartElem.prop('offsetWidth');
      var heightCart = cartElem.prop('offsetHeight');
      var imgElem = angular.element(event.target.parentNode.parentNode.childNodes[1]);
      var parentElem = angular.element(event.target.parentNode.parentNode);
      var offsetLeft = imgElem.prop("offsetLeft");
      var offsetTop = imgElem.prop("offsetTop");
      var imgSrc = imgElem.prop("currentSrc");
      console.log(offsetLeft + ' ' + offsetTop + ' ' + imgSrc);
      var imgClone = angular.element('<img src="' + imgSrc + '"/>');
      imgClone.css({
        'height': '150px',
        'position': 'absolute',
        'top': offsetTop + 'px',
        'left': offsetLeft + 'px',
        'opacity': 0.5
      });
      imgClone.addClass('itemaddedanimate');
      parentElem.append(imgClone);
      setTimeout(function () {
        imgClone.css({
          'height': '75px',
          'top': (offsetTopCart+heightCart/2)+'px',
          'left': (offsetLeftCart+widthCart/2)+'px',
          'opacity': 0.5
        });
      }, 500);
      setTimeout(function () {
        imgClone.css({
          'height': 0,
          'opacity': 0.5

        });
        cartElem.addClass('shakeit');
      }, 1000);
      setTimeout(function () {
        cartElem.removeClass('shakeit');
        imgClone.remove();
      }, 1500);
    });
  };


  return {
    restrict: 'E',
    link: link,
    transclude: true,
    replace: true,
    scope: {},
    template: '<button class="add-to-cart" ng-transclude></button>'
  };
});