在ionic App中遇到一個(gè)文字上下循環(huán)滾動(dòng)的效果實(shí)現(xiàn)缨硝,網(wǎng)上查了之后才知道有個(gè)通俗的名字-跑馬燈唆香。
這里借助了jQuery庫(kù)的選擇器和動(dòng)畫函數(shù)步氏,并且把jQuery的操作封裝到指令里虐块。先看指令代碼:
angular.module('starter')
.directive('slideScroll', function ($window, $timeout) {
return {
restrict: 'AE',
link: function (scope, element, attr) {
var _scrollHeight = 40;
var _newsLen = 3;
var index = 0;
setInterval(function () {
index += 1;
if (index > _newsLen) {
index = 0;
$(".news-right ul").css({
top: 0
})
} else {
$(".news-right ul").animate({
top: -_scrollHeight * index - 10 * index
}, 500);
}
}, 2000)
}
};
});
滾動(dòng)的高度scrollHeight設(shè)置為40px俩滥,三組文字newsLen循環(huán),每組兩行文字贺奠。每隔2000ms霜旧,ul列表向上移動(dòng)固定距離,top值為(_scrollHeight + 10)* index 的長(zhǎng)度儡率。
Html 代碼是這樣的:
<div class="news-right" ui-sref="newsList">
<ul slide-scroll>
<li class="news-box" ng-repeat="row in dataArr">
<p ng-repeat="item in row">{{item.title.length <= 19 ? item.title : item.title.slice(0, 19) + '...'}}</p>
</li>
<li>
<p ng-repeat="item1 in dataArr[0]">{{item1.title.length <= 19 ? item1.title : item1.title.slice(0, 19) + '...'}}</p>
</li>
</ul>
</div>
這里對(duì)文字做了簡(jiǎn)單的處理挂据,字符串超過19,會(huì)以“...”的形式顯示儿普。
Css 樣式表是這樣的:
.news-right {
position: absolute;
height: 40px;
left: 100px;
top: 10px;
right: 0;
color: rgb(65, 65, 65);
overflow: hidden;
}
.news-right ul{
width: 100%;
position: absolute;
top: 0;
left: 0;
}
.news-right p {
padding: 0;
line-height: 15px;
text-overflow: ellipsis;
box-sizing: border-box;
white-space: nowrap;
font-size: 13px;
}
效果圖就不再貼了