JSFiddle - React, Tailwind, and code Playground
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js"></script>
<div ng-app="myApp" ng-controller="WordsController">
<div class="rotator">
<p>{{sentence.static}} </p>
<text-rotator options="sentence.options"></text-rotator>
</div>
</div>
CSS
@import url(http://fonts.googleapis.com/css?family=Open+Sans:600);
body {
font-family: 'Open Sans', sans-serif;
font-weight: 600;
font-size: 40px;
}
.text {
position: absolute;
width: 450px;
left: 50%;
margin-left: -225px;
height: 40px;
top: 50%;
margin-top: -20px;
}
p {
display: inline-block;
vertical-align: top;
margin: 0;
}
.word {
position: absolute;
width: 220px;
opacity: 0;
}
.letter {
display: inline-block;
position: relative;
float: left;
transform: translateZ(25px);
transform-origin: 50% 50% 25px;
}
.letter.out {
transform: rotateX(90deg);
transition: transform 0.32s cubic-bezier(0.55, 0.055, 0.675, 0.19);
}
.letter.behind {
transform: rotateX(-90deg);
}
.letter.in {
transform: rotateX(0deg);
transition: transform 0.38s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.wisteria {
color: #8e44ad;
}
.belize {
color: #2980b9;
}
.pomegranate {
color: #c0392b;
}
.green {
color: #16a085;
}
.midnight {
color: #2c3e50;
}
JavaScript
angular.module('myApp', []);
angular.module('myApp').directive('textRotator', function ($interval) {
return {
restrict: 'E',
scope: {
options: '='
},
template: '<span class="word" ng-repeat="item in options">{{item}}</span>',
link: function (scope, element, attrs) {
console.log("Length : " + scope.options.length);
var i = 0;
// TODO: animate
stop = $interval(function () {
element.children().css('opacity', 0);
element.children().eq(i++ % 5).css('opacity', 0.8);
}, 1000)
scope.$on('$destroy', function() {
// Make sure that the interval is destroyed too
$interval.cancel(stop);
});
}
}
});
angular.module('myApp').controller('WordsController', function ($scope, $timeout) {
$scope.sentence = {static: 'Nachos are'};
// should be resolved by the router, and injected in this controller
// that way, words are ready when the controller is instantiated
// and when the link function of the directive is triggered
$scope.sentence.options = ['tasty', 'wonderful', 'fancy', 'beautiful', 'cheap'];
});