微信小程序 彈幕功能簡單實(shí)例
1、微信小程序----彈幕的實(shí)現(xiàn)(無后臺(tái))
小程序剛剛出來,現(xiàn)在網(wǎng)上的demo是多,但是要找到一個(gè)自己需要的卻不容易。今天跟大家分享自己寫的一個(gè)彈幕功能。
效果圖:

我的思路是這樣的,先用<switch>標(biāo)簽確定是否打開彈幕,若打開彈幕則出現(xiàn)彈幕文本框和發(fā)射按鈕,還有彈幕遮罩層。
先貼wxml和wxss代碼。
wxml代碼如下:
<!-- pages/index/index.wxml -->
<swiper indicator-dots="{{indicatorDots}}"
autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
<block wx:for="{{imgUrls}}" wx:key="unique">
<swiper-item>
<image src="{{item}}" class="slide-image"/>
</swiper-item>
</block>
</swiper>
<!--彈幕開關(guān)-->
<view class="barrage-Switch" style="color:{{barrageTextColor}};">
<switch id="switch_" bindchange="barrageSwitch"/>
<text>彈幕</text>
</view>
<!--彈幕輸入框-->
<view class="barrage-inputText" style="display:{{barrage_inputText}}">
<view class="barrage-input">
<input bindblur="bind_shoot" value="{{bind_shootValue}}"/>
</view>
<view class="barrage-shoot">
<button class="shoot" size="mini" bindtap="shoot">發(fā)射</button>
</view>
</view>
<!--彈幕上單文字-->
<view class="barrage-fly" style="display:{{barragefly_display}}">
<block wx:for="{{barrage_style}}" wx:key="unique">
<text class="barrage-textFly" style="color:{{item.barrage_shoottextColor}};left:{{item.barrage_phoneWidth}}px;top:{{item.barrageText_height}}px;">{{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)了。接下來要對彈幕上的字進(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)聽頁面加載
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);
},
//是否打開彈幕...
barrageSwitch: function(e){
console.log(e);
//先判斷沒有打開
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",
});
//打開定時(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)起來
// this.timer= setInterval(this.barrageText_move,800);
},
//定時(shí)器 讓彈幕動(dòng)起來
barrageText_move: function(){
var timerNum = barrage_style_arr.length;
var textMove ;
for(var i=0;i<timerNum;i++){
textMove = barrage_style_arr[i].barrage_phoneWidth;
console.log("barrage_style_arr["+i+"].barrage_phoneWidth----------:"+barrage_style_arr[i].barrage_phoneWidth);
textMove = textMove -20;
barrage_style_arr[i].barrage_phoneWidth = textMove;
//走完的移除掉
if(textMove<=-100){
// clearTimeout(this.timer);
barrage_style_arr.splice(0,1);
i--;
//全部彈幕運(yùn)行完
if(barrage_style_arr.length==0){
this.setData({
barrage_style:barrage_style_arr,
})
// clearInterval(this.timer);
return;
}
}
console.log("第"+i+"個(gè)定時(shí)器:",textMove);
this.setData({
barrage_style:barrage_style_arr,
})
}
},
//綁定發(fā)射輸入框,將值傳遞給data里的bind_shootValue,發(fā)射的時(shí)候調(diào)用
bind_shoot:function(e){
this.setData({
bind_shootValue:e.detail.value
})
},
})
因?yà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、其中最大的問題是如何讓文字跑起來,因?yàn)樾〕绦虿恢С謏Query,讓我這個(gè)js白癡有點(diǎn)無能為力。
在這說說自己讓文字移動(dòng)的思路:
首先,在打開彈幕的時(shí)候定義一個(gè)定時(shí)器,關(guān)閉的彈幕的時(shí)候把定時(shí)器給關(guān)掉。因?yàn)槲沂怯脭?shù)組來存儲(chǔ)文字移動(dòng)的樣式,其他確定下來。我只要改變left的大小就可以讓文字移動(dòng)。所以我用for循環(huán),當(dāng)定時(shí)器運(yùn)行的時(shí)候改變彈幕文字樣式 left:xxx px;的大小。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
微信小程序 tabs選項(xiàng)卡效果的實(shí)現(xiàn)
這篇文章主要介紹了微信小程序 tabs選項(xiàng)卡效果的實(shí)現(xiàn)的相關(guān)資料,微信小程序內(nèi)部組件沒有Tabs 選項(xiàng)卡的功能,自己實(shí)現(xiàn)個(gè)類似的,需要的朋友可以參考下2017-01-01
微信小程序 picker-view 組件詳解及簡單實(shí)例
這篇文章主要介紹了微信小程序 picker-view 組件詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01
ResizeObserver 監(jiān)視 DOM大小變化示例詳解
這篇文章主要為大家介紹了ResizeObserver 監(jiān)視 DOM大小變化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
JS面試中你不知道的call apply bind方法及使用場景詳解
這篇文章主要為大家介紹了JS面試中你不知道的call apply bind方法及使用場景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
JavaScript生成器函數(shù)Generator?Functions優(yōu)缺點(diǎn)特性詳解
這篇文章主要為大家介紹了JavaScript生成器函數(shù)Generator?Functions的特性及優(yōu)點(diǎn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
微信小程序之前臺(tái)循環(huán)數(shù)據(jù)綁定
這篇文章主要介紹了微信小程序之前臺(tái)循環(huán)數(shù)據(jù)綁定的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08

