The tag does a very cool thing in a very old-fashioned way and many programmers recommend not using it at all. Moreover, it has some issues with mobile devices. I was building a website that didn’t use jQuery, but used AngularJS and I needed to create a running news feed and the results were outstanding. I also took care of the SEO part. In order not to ruin Google’s page rank, the links contain ‘rel=”nofollow”‘ and in order to leave the visitor on my page I use ‘target=”_blank”‘ for the news to open in a new window. So here is the code… HTML (within the controller scope):
<div id="news_strip" ng-mouseover="conf.news_move_flag=false" ng-mouseout="conf.news_move_flag=true">
    <div class="shadow left">&nbsp;</div>
    <a ng-repeat="(k, n) in news track by $index" ng-style="{'right': get_news_right($index)}" id="news_{{$index}}" rel="nofollow" target="_blank" href="{{n.link}}">
        <span class="date">{{n.pubDate}}</span>
        <span class="title">{{n.title}}</span>
    </a>
    <div class="shadow right">&nbsp;</div>
</div>

CSS:

#news_strip {
  width: 100%;
  border-bottom: 1px dotted #cccccc;
  display: block;
  height: 17px;
  position: relative;
  overflow: hidden;
}
#news_strip a {
  display: inline;
  text-decoration: none;
  margin-right: 20px;
  z-index: 1;
  position: absolute;
  top: px;
}
#news_strip a span {
  cursor: pointer;
  display: inline;
  color: #343434;
  font-weight: bold;
  padding-right: 5px;
  z-index: 1;
}
#news_strip a span.date {
  display: inline-block;
  font-weight: normal;
  color: #888888;
}
#news_strip a:hover span.title {
  text-decoration: underline;
}
/* The shadow DIVs of the sides are decorative and optional, but they add a very nice effect :) */
#news_strip div.shadow{
  position: absolute;
  top: px;
  width: 30px;
  height: 100%;
  z-index: 5;
  background: #ffffff;
}
#news_strip div.shadow.left{
  left: px;
  -webkit-box-shadow: 10px px 10px #fff;
  -moz-box-shadow: 10px px 10px #fff;
  box-shadow: 10px px 10px #fff;
}
#news_strip div.shadow.right{
  right: px;
  -webkit-box-shadow: -10px px 10px #fff;
  -moz-box-shadow: -10px px 10px #fff;
  box-shadow: -10px px 10px #fff;
}

The most interesting part – AngularJS (within the controller scope) :

$scope.news = [];
$scope.conf = {
    news_length: false,
    news_pos: 200, // the starting position from the right in the news container
    news_margin: 20,
    news_move_flag: true
};

$scope.init = function() {
    $http.post('the_news_file.json', null).success(function(data) {
        if (data && data.length > ) {
            $scope.news = data;
            $interval($scope.news_move ,50);
        }
    });
};

$scope.get_news_right = function(idx) {
    var $right = $scope.conf.news_pos;
    for (var ri=; ri < idx; ri++) {
        if (document.getElementById('news_'+ri)) {
            $right += $scope.conf.news_margin + angular.element(document.getElementById('news_'+ri))[].offsetWidth;
        }
    }
    return $right+'px';
};

$scope.news_move = function() {
    if ($scope.conf.news_move_flag) {
        $scope.conf.news_pos--;
        if ( angular.element(document.getElementById('news_0'))[].offsetLeft > angular.element(document.getElementById('news_strip'))[].offsetWidth + $scope.conf.news_margin ) {
            var first_new = $scope.news[];
            $scope.news.push(first_new);
            $scope.news.shift();
            $scope.conf.news_pos += angular.element(document.getElementById('news_0'))[].offsetWidth + $scope.conf.news_margin;
        }
    };

And that’s it

  • About
  • Latest Posts

Written by Evgeny Kol Yakov

Related Posts

Find out more about:  A New Year's Message from Orly, CEO at WiseStamp (and a free Christmas app for your email signature)