[フレーム]
Last Updated: February 25, 2016
·
10.22K
· Evgeny Kolyakov

AngularJS running string (marquee alternative)

The <marquee> 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: 0px;
}

#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: 0px;
 width: 30px;
 height: 100%;
 z-index: 5;
 background: #ffffff;
}

#news_strip div.shadow.left{
 left: 0px;
 -webkit-box-shadow: 10px 0px 10px #fff;
 -moz-box-shadow: 10px 0px 10px #fff;
 box-shadow: 10px 0px 10px #fff;
}

#news_strip div.shadow.right{
 right: 0px;
 -webkit-box-shadow: -10px 0px 10px #fff;
 -moz-box-shadow: -10px 0px 10px #fff;
 box-shadow: -10px 0px 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 > 0) {
 $scope.news = data;
 $interval($scope.news_move ,50);
 }
 });
};

$scope.get_news_right = function(idx) {
 var $right = $scope.conf.news_pos;
 for (var ri=0; ri < idx; ri++) {
 if (document.getElementById('news_'+ri)) {
 $right += $scope.conf.news_margin + angular.element(document.getElementById('news_'+ri))[0].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'))[0].offsetLeft > angular.element(document.getElementById('news_strip'))[0].offsetWidth + $scope.conf.news_margin ) {
 var first_new = $scope.news[0];
 $scope.news.push(first_new);
 $scope.news.shift();
 $scope.conf.news_pos += angular.element(document.getElementById('news_0'))[0].offsetWidth + $scope.conf.news_margin;
 }
 }
};

And that's it :)

You can find it running in https://useful.co.il

7 Responses
Add your response

Link is not working

over 1 year ago ·

Sorry the site got closed ... It was a website I built for a friend and I wanted to build it purely on AngularJS.

Nevertheless, the code still works ^_^

over 1 year ago ·
over 1 year ago ·

How do i get a loop?

over 1 year ago ·

Can you please explain?
Because it does loop... even on my nexus5 :)

over 1 year ago ·

if i change ng-style="{'right': to left, i dont get the loop

over 1 year ago ·

if i change ng-style="{'right': to left, i dont get the loop

over 1 year ago ·

AltStyle によって変換されたページ (->オリジナル) /