国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > CSS > 正文

CSS3 實(shí)現(xiàn)彈幕的示例代碼

2024-07-11 09:07:48
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

最近需要項(xiàng)目需要實(shí)現(xiàn)彈幕,網(wǎng)上參考了各種方法,最后覺(jué)得transform+transition實(shí)現(xiàn)的效果在移動(dòng)設(shè)備上性能最好,在iphone6和紅米4上測(cè)試,看不到卡頓的感覺(jué)。用jquery的animate動(dòng)畫(huà)在移動(dòng)設(shè)備上有明顯的卡頓。

1.首先創(chuàng)建彈幕區(qū)域

<div class="barrage"><div class="mask"><!--//彈幕內(nèi)容--></div></div><input type="text" ng-model="data.comment"/><button ng-click="addDanmu()">說(shuō)兩句</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根據(jù)自己需求來(lái)即可

3.js

$scope.data = {comment:''};$scope.danmuCount = 5; //最大彈幕行數(shù)$scope.danmus = ['1545466666還是','9777777','哈哈哈哈哈','對(duì)企業(yè)讀完后環(huán)球網(wǎng)好齊齊哈','42115我我我5','556噢噢噢噢45','54哦','54545646','666但近段時(shí)間66','56565','454465465565', '1545466666還是','9777777','哈哈哈哈哈','對(duì)企業(yè)讀完后環(huán)球網(wǎng)好齊齊哈','42115我我我5','556噢噢噢噢45','54哦','54545646','666但近段時(shí)間66','56565','454465465565']; //彈幕數(shù)據(jù)源//創(chuàng)建彈幕區(qū)域$scope.createDanmuContent = function () {var height = 1 / $scope.danmuCount * 100 + '%';for (var i = 0; i < $scope.danmuCount; i++) {var item = '<div style="width: 100%; height: '+height+'"></div>';$('.mask').append(item);} $scope.createDanmu ();};//開(kāi)始彈幕繪制$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='margin: 0;position: absolute;opacity:1;display:table;left: "+_left + 'px'+';color:'+$scope.getRandomColor()+"'>"+$scope.danmus[i]+"</p>");$(".mask div").each(function () {//檢測(cè)該區(qū)域是否繪制了彈幕if ($scope.checkDanmu($(this))) {$(this).append(_lable);$scope.moveArray(i); i--;return false;}});}$scope.init_barrage();};//將數(shù)組第一位放到最后一位,(因彈幕池內(nèi)容太少,所以沒(méi)刪除已顯示的彈幕)$scope.moveArray = function (i) { var temp = $scope.danmus[i];$scope.danmus.splice(i,1);$scope.danmus.push(temp);}//判斷content區(qū)域有沒(méi)有彈幕$scope.checkDanmu = function (el) {return el.find('p').length == 0 ? true : false;};//獲取隨機(jī)顏色$scope.getRandomColor = function () {return '#' + (function(h){return new Array(7 - h.length).join("0") + h})((Math.random() * 0x1000000 << 0).toString(16))};//初始化彈幕參數(shù)$scope.init_barrage = function () {$(".mask div p").show().each(function() { var _moveLeft = window.screen.width+$(this).width();var time = 100000 / $(this).width() + 5000;//彈幕滑動(dòng)時(shí)間$scope.addCssAnimate($(this),_moveLeft,time);});};//添加彈幕動(dòng)畫(huà)$scope.addCssAnimate = function (el,_moveLeft,time) {el.css({'transform':'translateX('+-_moveLeft+'px)','transition':'all '+time+'ms'+ ' linear','-webkit-transform':'translateX('+-_moveLeft+'px)','-webkit-transition':'all '+time+'ms'+ ' linear','-moz-transform':'translateX('+-_moveLeft+'px)','-moz-transition':'all '+time+'ms'+ ' linear','-ms-transform':'translateX('+-_moveLeft+'px)','-ms-transition':'all '+time+'ms'+ ' linear'});//當(dāng)動(dòng)畫(huà)執(zhí)行完畢后,將彈幕移到原處,更換彈幕文字,重新開(kāi)始執(zhí)行動(dòng)畫(huà),相當(dāng)于對(duì)原本彈幕的復(fù)用$timeout(function () {//判斷彈幕池是否還有內(nèi)容,如果沒(méi)有則移除彈幕if ($scope.danmus.length > 0) {el.css({'transform':'translateX(0px)','transition':'all 0ms linear','-webkit-transform':'translateX(0px)','-webkit-transition':'all 0ms linear','-moz-transform':'translateX(0px)','-moz-transition':'all 0ms linear','-ms-transform':'translateX(0px)','-ms-transition':'all 0ms linear'});$scope.resetAnimate(el);} else {el.remove();}},time);};//更換彈幕內(nèi)容,重新開(kāi)始彈幕動(dòng)畫(huà)$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);};//評(píng)論,添加彈幕$scope.addDanmu = function () {var text = $scope.data.comment;if(text == ""){return;}$scope.danmus.unshift(text);};$scope.createDanmuContent();

至此,功能基本實(shí)現(xiàn)了。要關(guān)閉彈幕只需移除彈幕的區(qū)域,文中就沒(méi)有寫(xiě)了。

邏輯:首先根據(jù)$scope.danmuCount來(lái)創(chuàng)建彈幕的行數(shù),然后在每行里面添加彈幕,并添加相應(yīng)的動(dòng)畫(huà)。當(dāng)一個(gè)動(dòng)畫(huà)執(zhí)行完畢后,將彈幕移回原處,更換彈幕內(nèi)容,重新執(zhí)行動(dòng)畫(huà),這樣避免了彈幕重疊。每條彈幕動(dòng)畫(huà)執(zhí)行時(shí)間是根據(jù)彈幕長(zhǎng)度決定的。

個(gè)人經(jīng)驗(yàn),希望大家指出不足。上述代碼使用的angularjs,但邏輯都是一樣

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到CSS教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 渭源县| 涞源县| 元阳县| 张家界市| 綦江县| 青岛市| 互助| 林口县| 珠海市| 武夷山市| 丹巴县| 涿鹿县| 平湖市| 泰安市| 奉新县| 永济市| 平陆县| 牟定县| 德化县| 仁布县| 南阳市| 岳普湖县| 邻水| 无锡市| 洛阳市| 南宁市| 苗栗市| 含山县| 陆良县| 鹤庆县| 始兴县| 阿坝县| 三明市| 河西区| 永新县| 巴楚县| 和林格尔县| 都昌县| 宁乡县| 屏南县| 石狮市|