
When i was developing an app i wanted to add animation into my app such like breaking news popping into a small bar and slowly slide out. Here i am sharing my code for animation which is used as one of the angular2 component, just plug in play. To achieve this create one directory named animated-rolling under app/components, Create one animated.rolling.component .css, .html, and .ts file. Lets start with animated.rolling.component.ts file where we setup our animation component. here is the source code that
import {Component, ChangeDetectionStrategy,Input} from ‘@angular/core’;
import {CORE_DIRECTIVES} from ‘@angular/common’;
/*Angular 2 Tabs Example*/
@Component({
selector: ‘animatated-rolling’,
templateUrl: ‘app/components/animated-rolling/animated.rolling.component.html’,
styleUrls: [‘app/components/animated-rolling/animated.rolling.component.css’],
directives: [CORE_DIRECTIVES],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AnimatedRollingComponent {
@Input() brkNews:Array=[];
}
In the above code i am trying yo display a animated banner to display sports, it can be anything in your case.
In animated.rolling.component.css file add below code, this is very important because animation is achieved mainly by css.
.breaking-news-headline {
margin-left: 20px;
display: table-cell;
position: relative;
font-family: arial;
font-size: 13px;
margin-top: -22px;
color: white;
border: 2px solid white;
/* display: block; */
flex-direction: row;
justify-content: space-between;
text-align: center;
margin: 0 auto;
}
.breaking-news-title {
border: 2px solid yellow;
background-color: #FFEA00;
/* display: block; */
/* height: 20px; */
/* width: 90px; */
font-family: arial;
font-size: 11px;
position: absolute;
top: 0px;
margin-top: 0px;
margin-left: 20px;
padding-top: 10px;
padding-left: 10px;
z-index: 3;
&:before {
content:””;
position: absolute;
display: block;
width: 0px;
height: 0px;
top: 0;
left: -12px;
border-left:12px solid transparent;
border-right: 0px solid transparent;
border-bottom: 30px solid #FFEA00;
}
&:after {
content:””;
position: absolute;
display: block;
width: 0px;
height: 0px;
right: -12px;
top: 0;
border-right:12px solid transparent;
border-left: 0px solid transparent;
border-top: 30px solid #FFEA00;
}
}
#breaking-news-colour {
height: 30px;
/* width: 100%; */
background-color: grey;
/* position: relative;
*/
}
#breaking-news-container {
transition-delay: 2s;
/* margin-left: 10px; */
display: table;
flex-direction: row;
justify-content: space-between;
/* margin: 0 auto; */
float:left;
/* border: 2px solid greenyellow; */
background-color: #3399FF;
position: relative;
height: 100%;
width: 100%;
overflow: hidden;
/* position: absolute; */
&:before {
content: “”;
width: 3px;
height: 3px;
background-color: #3399FF;
position: absolute;
z-index: 2;
}
}
.animated {
-webkit-animation-duration: 0.2s;
-webkit-animation-fill-mode: both;
-moz-animation-duration: 0.2s;
-moz-animation-fill-mode: both;
-webkit-animation-iteration-count: 1;
-moz-animation-iteration-count: 1;
}
.delay-animated {
-webkit-animation-duration: 0.4s;
-webkit-animation-fill-mode: both;
-moz-animation-duration: 0.4s;
-moz-animation-fill-mode: both;
-webkit-animation-iteration-count: 1;
-moz-animation-iteration-count: 1;
-webkit-animation-delay: 0.3s;
animation-delay: 0.3s;
}
.scroll-animated {
-webkit-animation-duration: 3s;
-webkit-animation-fill-mode: both;
-moz-animation-duration: 3s;
-moz-animation-fill-mode: both;
-webkit-animation-iteration-count: 1;
-moz-animation-iteration-count: 1;
-webkit-animation-delay: 0.5s;
animation-delay: 0.5s;
}
.delay-animated2 {
-webkit-animation-duration: 0.4s;
-webkit-animation-fill-mode: both;
-moz-animation-duration: 0.4s;
-moz-animation-fill-mode: both;
-webkit-animation-iteration-count: 1;
-moz-animation-iteration-count: 1;
-webkit-animation-delay: 0.5s;
animation-delay: 0.5s;
}
.delay-animated3 {
-webkit-animation-duration: 5s;
-webkit-animation-fill-mode: both;
-moz-animation-duration: 5s;
-moz-animation-fill-mode: both;
-webkit-animation-iteration-count: 1;
-moz-animation-iteration-count: 1;
-webkit-animation-delay: 0.5s;
animation-delay: 3s;
}
.fadein {
-webkit-animation-name: fadein;
-moz-animation-name: fadein;
-o-animation-name: fadein;
animation-name: fadein;
}
@-webkit-keyframes fadein {
from {
margin-left: 1000px
}
to {
}
}
@-moz-keyframes fadein {
from {
margin-left: 1000px
}
to {
}
}
.slidein {
-webkit-animation-name: slidein;
-moz-animation-name: slidein;
-o-animation-name: slidein;
animation-name: slidein;
}
@keyframes marquee {
0% {
left: 0;
}
20% {
left: 0;
}
100% { left: -100%; }
}
.marquee {
animation: marquee 10s linear infinite;
-webkit-animation-duration: 10s;
-moz-animation-duration: 10s;
-webkit-animation-delay: 0.5s;
animation-delay: 3s;
}
@-webkit-keyframes slidein {
from {
margin-left: 800px
}
to {
margin-top: 0px
}
}
@-moz-keyframes slidein {
from {
margin-left: 800px
}
to {
margin-top: 0px
}
}
.slideup {
-webkit-animation-name: slideup;
-moz-animation-name: slideup;
-o-animation-name: slideup;
animation-name: slideup;
}
@-webkit-keyframes slideup {
from {
margin-top: 30px
}
to {
margin-top: 0;
}
}
@-moz-keyframes slideup {
from {
margin-top: 30px
}
to {
margin-top: 0;
}
}
Now we need to add html which will take data from ts and styling from css file. Here is the source code for animated.rolling.component.html
Above is very little code to make your animated banner. We are transporting brkNews variable from ts file which would have list of sports and displaying it. Now as we want this component to be independent and any data can be fed from parent component such as app.component or some other containing component. So here is what you have to do
Add your component to parent component by import statement
import {AnimatedRollingComponent} from ‘../animated-rolling/animated.rolling.component’
Create a variable in parent component which will have list of data/objects like below.
public slideNews:Array=[
{title:”Games”,href:”https://www.google.co.uk”},
{title:”FIFA”, href:”https://accounts.google.com”},
{title:”Tennis”, href:”https://en.wikipedia.org/wiki/Novak_Djokovic”},
{title:”BBC”, href:”http://www.bbc.co.uk/sport/tennis”}
]
Also add your child component as a directive into your parent component.
directives: […,AnimatedRollingComponent],
Now the last step where you bridge your child component with parent component. Assign your list of data/object to Input variable of child component. This will take data from parent to child and display in the html rendering that. So in parent html you have to do this.
Now its ready for execution. IT will look like this.

Here you have your breaking news animation on Angular2.