I am trying to implement a custom directive <ticker>
which will allow the text in the element to scroll on mouseover like a news ticker if the text width exceeds the space available in the element.
The directive works with normal text like this:
<ticker>A Very Very Long Test Statement</ticker>
but not with a binded variable like this:
<ticker>{{item}}</ticker>
I think it is because I try to strip out the html text in the directive. you can see in this PLUNKER
Here is my full directive:
angular.module('app.directives.ticker', [])
.directive('ticker', function($interval){
return {
restrict: 'E',
scope: {},
link: function(scope, element, attrs){
var counter = 0;
var active = false;
var needed = false;
var firstIteration = true;
var originalText = element.html();
var elementWidth = element.width();
var textWidth = getTextWidth(element);
if(textWidth > elementWidth){
needed = true;
$interval(startTicker, 25);
}
function startTicker(){
if(firstIteration === true){
element.html("");
element.append("<span id='firstTickerItem' class='tickerItem'>" + originalText + "</span>");
element.append("<span id='secondTickerItem'class='tickerItem'>" + originalText + "</span>");
firstIteration = false;
}
if(counter > textWidth + 19){
$('#firstTickerItem').remove();
$('#secondTickerItem').attr("id","firstTickerItem");
element.append("<span id='secondTickerItem'class='tickerItem'>" + originalText + "</span>");
counter = 0;
}
if(active && needed){
var leftMostMargin = parseInt($('#firstTickerItem').css('margin-left').replace("px", ""));
$('#firstTickerItem').css({'margin-left': leftMostMargin - 1 + "px"});
counter += 1;
}
}
element.mouseenter(function(){
active = true;
firstIteration = true;
});
element.mouseleave(function(){
active = false;
element.html("");
element.html(originalText);
counter = 0;
});
}
};
});
How can I make this work?