首頁  >  文章  >  web前端  >  實例詳解CSS3 實現彈幕

實例詳解CSS3 實現彈幕

小云云
小云云原創
2018-05-15 14:21:042630瀏覽

專案需要實現彈幕,網路上參考了各種方法,最後覺得transform+transition實現的效果在行動裝置上表現最好,在iphone6和紅米4上測試,看不到卡頓的感覺。用jquery的animate動畫在行動裝置上有明顯的卡頓。本文主要介紹了CSS3 實現彈幕的範例程式碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧,希望能幫助大家。

1.首先建立彈幕區域

<p class="barrage">
<p class="mask">
<!--//弹幕内容-->
</p>
</p>

<input type="text" ng-model="data.comment"/>
<button ng-click="addDanmu()">说两句</button >

#2.css

.webPage .barrage{width:100%;height:22%;position: absolute; bottom: 50px; 
background-color: transparent;pointer-events: none;
}
.webPage .barrage .mask{width:100%;height:100%;background:transparent;z-index:100;}

註:以上html, css依照自己需求來即可

3.js

$scope.data = {comment:&#39;&#39;};
$scope.danmuCount = 5; //最大弹幕行数
$scope.danmus = [&#39;1545466666还是&#39;,&#39;9777777&#39;,&#39;哈哈哈哈哈&#39;,&#39;对企业读完后环球网好齐齐哈&#39;,&#39;42115我我我5&#39;,&#39;556噢噢噢噢45&#39;,&#39;54哦&#39;,&#39;54545646&#39;,&#39;666但近段时间66&#39;,&#39;56565&#39;,&#39;454465465565&#39;, &#39;1545466666还是&#39;,&#39;9777777&#39;,&#39;哈哈哈哈哈&#39;,&#39;对企业读完后环球网好齐齐哈&#39;,&#39;42115我我我5&#39;,&#39;556噢噢噢噢45&#39;,&#39;54哦&#39;,&#39;54545646&#39;,&#39;666但近段时间66&#39;,&#39;56565&#39;,&#39;454465465565&#39;]; //弹幕数据源

//创建弹幕区域
$scope.createDanmuContent = function () {
var height = 1 / $scope.danmuCount * 100 + &#39;%&#39;;
for (var i = 0; i < $scope.danmuCount; i++) {
var item = &#39;<p style="width: 100%; height: &#39;+height+&#39;"></p>&#39;;
$(&#39;.mask&#39;).append(item);
}
 $scope.createDanmu ();
};

//开始弹幕绘制
$scope.createDanmu = function () {
var maxCount = 0;
if ($scope.danmus.length > $scope.danmuCount) {
 maxCount = $scope.danmuCount;
 } else {
maxCount = $scope.danmus.length;
}
var _left = window.screen.width;
for (var i = 0; i < maxCount; i++) {
var _lable = $("<p style=&#39;margin: 0;position: absolute;opacity:1;display:table;left: "+_left + &#39;px&#39;+&#39;;color:&#39;+$scope.getRandomColor()+"&#39;>"+$scope.danmus[i]+"</p>");
$(".mask p").each(function () {
//检测该区域是否绘制了弹幕
if ($scope.checkDanmu($(this))) {
$(this).append(_lable);
$scope.moveArray(i);
 i--;
return false;
}
});
}
$scope.init_barrage();
};

//将数组第一位放到最后一位,(因弹幕池内容太少,所以没删除已显示的弹幕)
$scope.moveArray = function (i) {
 var temp = $scope.danmus[i];
$scope.danmus.splice(i,1);
$scope.danmus.push(temp);
}
//判断content区域有没有弹幕
$scope.checkDanmu = function (el) {
return el.find(&#39;p&#39;).length == 0 ? true : false;
};
//获取随机颜色
$scope.getRandomColor = function () {
return &#39;#&#39; + (function(h){
return new Array(7 - h.length).join("0") + h
})((Math.random() * 0x1000000 << 0).toString(16))
};

//初始化弹幕参数
$scope.init_barrage = function () {
$(".mask p p").show().each(function() {
 var _moveLeft = window.screen.width+$(this).width();
var time = 100000 / $(this).width() + 5000;//弹幕滑动时间
$scope.addCssAnimate($(this),_moveLeft,time);
});
};

//添加弹幕动画
$scope.addCssAnimate = function (el,_moveLeft,time) {
el.css({
&#39;transform&#39;:&#39;translateX(&#39;+-_moveLeft+&#39;px)&#39;,
&#39;transition&#39;:&#39;all &#39;+time+&#39;ms&#39;+ &#39; linear&#39;,
&#39;-webkit-transform&#39;:&#39;translateX(&#39;+-_moveLeft+&#39;px)&#39;,
&#39;-webkit-transition&#39;:&#39;all &#39;+time+&#39;ms&#39;+ &#39; linear&#39;,
&#39;-moz-transform&#39;:&#39;translateX(&#39;+-_moveLeft+&#39;px)&#39;,
&#39;-moz-transition&#39;:&#39;all &#39;+time+&#39;ms&#39;+ &#39; linear&#39;,
&#39;-ms-transform&#39;:&#39;translateX(&#39;+-_moveLeft+&#39;px)&#39;,
&#39;-ms-transition&#39;:&#39;all &#39;+time+&#39;ms&#39;+ &#39; linear&#39;
});
//当动画执行完毕后,将弹幕移到原处,更换弹幕文字,重新开始执行动画,相当于对原本弹幕的复用
$timeout(function () {
//判断弹幕池是否还有内容,如果没有则移除弹幕
if ($scope.danmus.length > 0) {
el.css({
&#39;transform&#39;:&#39;translateX(0px)&#39;,
&#39;transition&#39;:&#39;all 0ms linear&#39;,
&#39;-webkit-transform&#39;:&#39;translateX(0px)&#39;,
&#39;-webkit-transition&#39;:&#39;all 0ms linear&#39;,
&#39;-moz-transform&#39;:&#39;translateX(0px)&#39;,
&#39;-moz-transition&#39;:&#39;all 0ms linear&#39;,
&#39;-ms-transform&#39;:&#39;translateX(0px)&#39;,
&#39;-ms-transition&#39;:&#39;all 0ms linear&#39;
});
$scope.resetAnimate(el);
} else {
el.remove();
}
},time);
};

//更换弹幕内容,重新开始弹幕动画
$scope.resetAnimate = function (el) {
el.html($scope.danmus[0]);
$scope.moveArray(0);
var _moveLeft = el.width() + screen.width;
var time = 100000 / el.width() + 5000;
$scope.addCssAnimate(el,_moveLeft,time);
};

//评论,添加弹幕
$scope.addDanmu = function () {
var text = $scope.data.comment;
if(text == ""){
return;
}
$scope.danmus.unshift(text);
};

$scope.createDanmuContent();

至此,功能基本上實作了。要關閉彈幕只要移除彈幕的區域,文中就沒有寫了。

邏輯:先根據$scope.danmuCount來建立彈幕的行數,然後在每行裡面加上彈幕,並加入對應的動畫。當一個動畫執行完畢後,將彈幕移回原處,更換彈幕內容,重新執行動畫,這樣避免了彈幕重疊。每條彈幕動畫執行時間是根據彈幕長度決定的。

個人經驗,希望大家指出不足。上述程式碼使用的angularjs,但邏輯都是一樣

相關推薦:

HTML5文字彈幕效果

微信小程式之彈幕的程式碼實作

html5遊戲開發-彈幕+仿雷電小遊戲demo

#

以上是實例詳解CSS3 實現彈幕的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn