小編給大家分享一下微信小程序中如何實(shí)現(xiàn)彈幕效果,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
我們提供的服務(wù)有:網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、蕭縣ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的蕭縣網(wǎng)站制作公司
1、微信小程序----彈幕的實(shí)現(xiàn)(無(wú)后臺(tái))
小程序剛剛出來(lái),現(xiàn)在網(wǎng)上的demo是多,但是要找到一個(gè)自己需要的卻不容易。今天跟大家分享自己寫(xiě)的一個(gè)彈幕功能。
效果圖:
我的思路是這樣的,先用<switch>標(biāo)簽確定是否打開(kāi)彈幕,若打開(kāi)彈幕則出現(xiàn)彈幕文本框和發(fā)射按鈕,還有彈幕遮罩層。
先貼wxml和wxss代碼。
wxml代碼如下:
<!-- pages/index/index.wxml --> <swiper indicator-dots="pw_indicatorDots" autoplay="pw_autoplay" interval="pw_interval" duration="pw_duration"> <block wx:for="pw_imgUrls" wx:key="unique"> <swiper-item> <image src="pw_item" class="slide-image"/> </swiper-item> </block> </swiper> <!--彈幕開(kāi)關(guān)--> <view class="barrage-Switch" style="color:pw_barrageTextColor;"> <switch id="switch_" bindchange="barrageSwitch"/> <text>彈幕</text> </view> <!--彈幕輸入框--> <view class="barrage-inputText" style="display:pw_barrage_inputText"> <view class="barrage-input"> <input bindblur="bind_shoot" value="pw_bind_shootValue"/> </view> <view class="barrage-shoot"> <button class="shoot" size="mini" bindtap="shoot">發(fā)射</button> </view> </view> <!--彈幕上單文字--> <view class="barrage-fly" style="display:pw_barragefly_display"> <block wx:for="pw_barrage_style" wx:key="unique"> <text class="barrage-textFly" style="color:pw_item.barrage_shoottextColor;left:pw_item.barrage_phoneWidthpx;top:pw_item.barrageText_heightpx;">pw_item.barrage_shootText</text> </block> </view>
wxss代碼如下:
/* pages/index/index.wxss */ .slide-image{ width: 100%; } /* 彈幕選擇按鈕的操作*/ .barrage-Switch{ position: absolute; bottom: 10px; right: 10px; z-index: 2; } /* 彈幕輸入框的操作*/ .barrage-inputText{ position: absolute; display: flex; background-color: #BFBFBF; width: 100%; height: 40px; flex-direction: row; nav-index: 2; justify-content: center; align-items: center; bottom: 10%; } .barrage-input{ background-color: greenyellow; width: 60%; height: 30px; } .barrage-shoot{ margin-left: 10px; width: 25%; height: 30px; } .shoot{ width: 100%; color: black; } /*彈幕飛飛飛*/ .barrage-fly{ z-index: 3; height: 80%; width: 100%; position: absolute; top: 0; } .barrage-textFly{ position: absolute; }
這樣基本的樣式就都實(shí)現(xiàn)了。接下來(lái)要對(duì)彈幕上的字進(jìn)行處理。
文字是從右往左移動(dòng),文字出現(xiàn)的位置top是隨機(jī),left則是取屏幕的寬度。移動(dòng)的時(shí)候是用定時(shí)器進(jìn)行處理。
還有就是字體的顏色是隨機(jī)出現(xiàn)的。這些功能都是利用js處理的。
js的代碼如下:
var barrage_style_arr = []; var barrage_style_obj ={}; var phoneWidth = 0; var timers = []; var timer ; Page({ data: { imgUrls: [ 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg', 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg', 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg' ], indicatorDots: true, autoplay: true, interval: 3000, duration: 500, barrageTextColor:"#D3D3D3", barrage_inputText:"none", barrage_shoottextColor:"black", bind_shootValue:"", barrage_style:[], barragefly_display:"none", }, // 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載 onLoad:function(options){ var that = this; //獲取屏幕的寬度 wx.getSystemInfo({ success: function(res) { that.setData({ barrage_phoneWidth:res.windowWidth-100, }) } }) phoneWidth = that.data.barrage_phoneWidth; console.log(phoneWidth); }, //是否打開(kāi)彈幕... barrageSwitch: function(e){ console.log(e); //先判斷沒(méi)有打開(kāi) if(!e.detail.value){ //清空彈幕 barrage_style_arr = []; //設(shè)置data的值 this.setData({ barrageTextColor:"#D3D3D3", barrage_inputText:"none", barragefly_display:"none", barrage_style:barrage_style_arr, }); //清除定時(shí)器 clearInterval(timer); }else{ this.setData({ barrageTextColor:"#04BE02", barrage_inputText:"flex", barragefly_display:"block", }); //打開(kāi)定時(shí)器 timer= setInterval(this.barrageText_move,800) } }, //發(fā)射按鈕 shoot: function(e){ //字體顏色隨機(jī) var textColor = "rgb("+parseInt(Math.random()*256)+","+parseInt(Math.random()*256)+","+parseInt(Math.random()*256)+")"; // //設(shè)置彈幕字體的水平位置樣式 // var textWidth = -(this.data.bind_shootValue.length*0); //設(shè)置彈幕字體的垂直位置樣式 var barrageText_height = (Math.random())*266; barrage_style_obj = { // textWidth:textWidth, barrageText_height:barrageText_height, barrage_shootText:this.data.bind_shootValue, barrage_shoottextColor : textColor, barrage_phoneWidth:phoneWidth }; barrage_style_arr.push(barrage_style_obj); this.setData({ barrage_style:barrage_style_arr, //發(fā)送彈幕 bind_shootValue:"" //清空輸入框 }) //定時(shí)器 讓彈幕動(dòng)起來(lái) // this.timer= setInterval(this.barrageText_move,800); }, //定時(shí)器 讓彈幕動(dòng)起來(lái) barrageText_move: function(){ var timerNum = barrage_style_arr.length; var textMove ; for(var i=0;i<timerNum;i++){ textMove = barrage_style_arr
因?yàn)閯倓偨佑|小程序,所以對(duì)一些語(yǔ)句的使用都不是很了解。所以遇到了一些問(wèn)題:
1、在js中獲取wxml的控件的信息。
js:
barrageSwitch: function(e){ console.log(e); }
wxml:
<switch id="switch_" bindchange="barrageSwitch"/>
結(jié)果:返回了一個(gè)objec.在控制臺(tái)返回的類型好像都是json格式的數(shù)據(jù)。
Object {type: "change", timeStamp: 2766, target: Object, currentTarget: Object, detail: Object}
2、在實(shí)現(xiàn)彈幕的時(shí)候,點(diǎn)擊發(fā)射按鈕,如何獲取到輸入框的信息。
在這,我是輸入框失去焦點(diǎn)的時(shí)候,將數(shù)據(jù)復(fù)制給js中的data類,再點(diǎn)擊發(fā)射的時(shí)候取data類中的值。
3、其中最大的問(wèn)題是如何讓文字跑起來(lái),因?yàn)樾〕绦虿恢С謏Query,讓我這個(gè)js白癡有點(diǎn)無(wú)能為力。
在這說(shuō)說(shuō)自己讓文字移動(dòng)的思路:
首先,在打開(kāi)彈幕的時(shí)候定義一個(gè)定時(shí)器,關(guān)閉的彈幕的時(shí)候把定時(shí)器給關(guān)掉。因?yàn)槲沂怯脭?shù)組來(lái)存儲(chǔ)文字移動(dòng)的樣式,其他確定下來(lái)。我只要改變left的大小就可以讓文字移動(dòng)。所以我用for循環(huán),當(dāng)定時(shí)器運(yùn)行的時(shí)候改變彈幕文字樣式 left:xxx px;的大小。
以上是“微信小程序中如何實(shí)現(xiàn)彈幕效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文標(biāo)題:微信小程序中如何實(shí)現(xiàn)彈幕效果
本文地址:http://chinadenli.net/article20/pdssco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、域名注冊(cè)、網(wǎng)站收錄、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)