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

首頁 > 編程 > JavaScript > 正文

微信小程序 彈幕功能簡單實(shí)例

2019-11-19 17:35:12
字體:
供稿:網(wǎng)友

1、微信小程序----彈幕的實(shí)現(xiàn)(無后臺)

小程序剛剛出來,現(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)行處理。

文字是從右往左移動,文字出現(xiàn)的位置top是隨機(jī),left則是取屏幕的寬度。移動的時(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í)器 讓彈幕動起來   // this.timer= setInterval(this.barrageText_move,800); },//定時(shí)器 讓彈幕動起來 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.在控制臺返回的類型好像都是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)無能為力。

在這說說自己讓文字移動的思路:

首先,在打開彈幕的時(shí)候定義一個(gè)定時(shí)器,關(guān)閉的彈幕的時(shí)候把定時(shí)器給關(guān)掉。因?yàn)槲沂怯脭?shù)組來存儲文字移動的樣式,其他確定下來。我只要改變left的大小就可以讓文字移動。所以我用for循環(huán),當(dāng)定時(shí)器運(yùn)行的時(shí)候改變彈幕文字樣式 left:xxx px;的大小。

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平邑县| 锡林浩特市| 鲁山县| 威信县| 鄂尔多斯市| 繁昌县| 台南县| 巩留县| 富顺县| 岳阳市| 乐清市| 神农架林区| 金门县| 靖远县| 松阳县| 确山县| 泸溪县| 罗山县| 杭锦旗| 西充县| 雅江县| 平乡县| 昌江| 喀什市| 临泽县| 运城市| 昌江| 忻州市| 沂源县| 哈密市| 定日县| 凌源市| 北安市| 临清市| 育儿| 阿尔山市| 福贡县| 滁州市| 景谷| 南昌市| 巴南区|