2016-03-03 43 views
0

难度越来越JavaScript动画与AngularJS指令工作

function percentToPixel(perc) { 
 
    return ($(".stepNavContainer").outerWidth()/100) * parseFloat(perc); 
 
} 
 

 
var app = angular.module('app', ["ngAnimate"]); 
 
app.controller("appController", function($scope) { 
 

 
}); 
 

 
app.directive("init-modal", function() { 
 
    return { 
 
    restrict: 'E', 
 
    link: function(scope, element, attrs) { 
 
     scope.init = function() { 
 
     TweenMax.set($("#cursor"), { 
 
      x: percentToPixel("0") + "px", 
 
      xPercent: -50 
 
     }); 
 
     TweenMax.set($("#joinModalNavStep1"), { 
 
      x: percentToPixel("0") + "px", 
 
      xPercent: -50 
 
     }); 
 
     TweenMax.set($("#joinModalNavStep2"), { 
 
      x: percentToPixel("50") + "px", 
 
      xPercent: -50 
 
     }); 
 
     TweenMax.set($("#joinModalNavStep3"), { 
 
      x: percentToPixel("100") + "px", 
 
      xPercent: -50 
 
     }); 
 
     }; 
 
    } 
 
    }; 
 
}); 
 

 

 

 
app.directive("step-modal", function() { 
 
    return { 
 
    restrict: 'E', 
 
    link: function(scope, element, attrs) { 
 
     var tlStepNavAnimation = new TimelineMax(); 
 
     tlStepNavAnimation.to(cursor, 1, { 
 
     x: percentToPixel("0") + "px", 
 
     xPercent: -50, 
 
     ease: Back.easeInOut 
 
     }); 
 
     tlStepNavAnimation.addPause(); 
 
     tlStepNavAnimation.to(cursor, 1, { 
 
     x: percentToPixel("50") + "px", 
 
     xPercent: -50, 
 
     ease: Back.easeInOut 
 
     }); 
 
     tlStepNavAnimation.addPause(); 
 
     tlStepNavAnimation.to(cursor, 1, { 
 
     x: percentToPixel("100") + "px", 
 
     xPercent: -50, 
 
     ease: Back.easeInOut 
 
     }); 
 

 
     scope.play = function() { 
 
     tlStepNavAnimation.play(); 
 
     }; 
 

 
     scope.reverse = function() { 
 
     tlStepNavAnimation.reverse(); 
 
     }; 
 
    } 
 
    }; 
 
});
html, 
 
body { 
 
    overflow: hidden; 
 
} 
 
body { 
 
    background-color: white; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
.stepNavContainer { 
 
    position: relative; 
 
    height: 50px; 
 
    width: 50%; 
 
    left: 25%; 
 
    border: 1px solid red; 
 
} 
 
.circle { 
 
    display: block; 
 
    position: absolute; 
 
    width: 50px; 
 
    height: 50px; 
 
    border-radius: 50%; 
 
} 
 
#cursor { 
 
    display: block; 
 
    position: absolute; 
 
    width: 50px; 
 
    height: 50px; 
 
    background: #c32026; 
 
    border-radius: 50%; 
 
} 
 
.step { 
 
    background: #999999; 
 
} 
 
.btn { 
 
    width: 100px; 
 
    height: 50px; 
 
    border: 1px solid black; 
 
}
<!DOCTYPE html> 
 
<html ng-app="app" ng-controller="appController"> 
 

 
<head> 
 
    <title>Title of the document</title> 
 
    <!-- Angular Material style sheet --> 
 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.0.6/angular-material.min.css"> 
 
    <link rel="stylesheet" href="style.css"> 
 
</head> 
 

 
<body> 
 

 
    <div init-modal ng-click="init()" id="setupBtn" class="btn"> 
 
    <span>Setup</span> 
 
    </div> 
 

 
    <div step-modal ng-click="reverse()" id="previousBtn" class="btn"> 
 
    <span>Previous</span> 
 
    </div> 
 

 
    <div id="nextBtn" class="btn"> 
 
    <span step-modal ng-click="play()">Next</span> 
 
    </div> 
 
    <div init-modal class="stepNavContainer"> 
 

 

 
    <span id="joinModalNavStep1" class="circle step"></span> 
 

 

 
    <span id="joinModalNavStep2" class="circle step"></span> 
 

 

 
    <span id="joinModalNavStep3" class="circle step"></span> 
 

 
    <span id="cursor" class="circle"></span> 
 

 
    </div> 
 

 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.2/TweenMax.min.js"></script> 
 
</body> 
 

 
</html>

所以,我有一个工作的JavaScript动画(使用GreenSock),你可以在这里看到http://codepen.io/jstafford/pen/VaLgvK。我试图将其插入到AngularJS指令中,但我甚至没有在安装程序(init-modal指令)或Next和Previous(step-modal指令)按钮的指令内进行。 1)按下Setup按钮,将三个圆和光标设置在触发init-modal指令的初始位置。2)然后按下Next按钮或Previoius按钮,触发step-modal指令,使step动画发生。我是AngularJS的新手,我正在尝试使用AngularJS的方式,但真的很挣扎。任何帮助不胜感激。

回答

0

首先,给骆驼案件名称到您的指令:

app.directive("initModal", function() { 
    return { 
    restrict: 'E', 
    link: function(){} 
    } 
} 

限制: 'E'=>元素:<init-modal></init-modal>

限制: 'A'=>属性:<div init-modal></div>

限制:'C'=>类别名称:<div class="init-modal"></div>

Angular's directive documentation

+0

非常感谢!骆驼案件和限制“A”是我需要它的工作。我很感激。 –