欢迎光临,了解微信小程序开发,就上易用通!

微信小程序----弹幕的实现(无后台)

发布:2018-01-25 09:44浏览: 来源:网络 作者:tianshu

小程序刚刚出来,现在网上的demo是多,但是要找到一个自己需要的却不容易。今天跟大家分享自己写的一个弹幕功能。
 
 

我的思路是这样的,先用<switch>标签确定是否打开弹幕,若打开弹幕则出现弹幕文本框和发射按钮,还有弹幕遮罩层。
先贴wxml和wxss代码。

wxml代码如下:
  1. <!-- pages/index/index.wxml -->
  2. <swiper indicator-dots="{{indicatorDots}}"
  3.   autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
  4.   <block wx:for="{{imgUrls}}" wx:key="unique">
  5.     <swiper-item>
  6.       <image src="{{item}}" class="slide-image"/>
  7.     </swiper-item>
  8.   </block>
  9. </swiper>
  10.  
  11. <!--弹幕开关-->
  12. <view class="barrage-Switch" style="color:{{barrageTextColor}};">
  13.     <switch id="switch_" bindchange="barrageSwitch"/>
  14.     <text>弹幕</text>
  15. </view>
  16.  
  17. <!--弹幕输入框-->
  18.   <view class="barrage-inputText" style="display:{{barrage_inputText}}">
  19.       <view class="barrage-input">
  20.         <input bindblur="bind_shoot" value="{{bind_shootValue}}"/>
  21.       </view>
  22.       <view class="barrage-shoot">
  23.           <button class="shoot" size="mini" bindtap="shoot">发射</button>
  24.       </view>
  25.   </view>
  26.  
  27. <!--弹幕上单文字-->
  28. <view class="barrage-fly" style="display:{{barragefly_display}}">
  29.   <block wx:for="{{barrage_style}}" wx:key="unique">
  30.    <text class="barrage-textFly" style="color:{{item.barrage_shoottextColor}};left:{{item.barrage_phoneWidth}}px;top:{{item.barrageText_height}}px;">{{item.barrage_shootText}}</text>
  31.   </block>
  32. </view>

wxss代码如下:
  1. /* pages/index/index.wxss */
  2. .slide-image{
  3.     width: 100%;
  4. }
  5.  
  6. /* 弹幕选择按钮的操作*/
  7. .barrage-Switch{
  8.     position: absolute;
  9.     bottom: 10px;
  10.     right: 10px;
  11.     z-index: 2;
  12. }
  13.  
  14. /* 弹幕输入框的操作*/
  15. .barrage-inputText{
  16.     position: absolute;
  17.     display: flex;
  18.     background-color: #BFBFBF;
  19.     width: 100%;
  20.     height: 40px;
  21.     flex-direction: row;
  22.     nav-index: 2;
  23.     justify-content: center;
  24.     align-items: center;
  25.     bottom: 10%;
  26. }
  27. .barrage-input{
  28.     background-color: greenyellow;
  29.     width: 60%;
  30.     height: 30px;
  31. }
  32. .barrage-shoot{
  33.  
  34.     margin-left: 10px;
  35.     width: 25%;
  36.     height: 30px;
  37. }
  38. .shoot{
  39.     width: 100%;
  40.     color: black;
  41. }
  42.  
  43. /*弹幕飞飞飞*/
  44. .barrage-fly{
  45.     z-index: 3;
  46.     height: 80%;
  47.     width: 100%;
  48.     position: absolute;
  49.     top: 0;
  50. }
  51. .barrage-textFly{
  52.     position: absolute;
  53.  
  54. }

这样基本的样式就都实现了。接下来要对弹幕上的字进行处理。
文字是从右往左移动,文字出现的位置top是随机,left则是取屏幕的宽度。移动的时候是用定时器进行处理。
还有就是字体的颜色是随机出现的。这些功能都是利用js处理的。

js的代码如下:
  1. var barrage_style_arr = [];
  2. var barrage_style_obj ={};
  3. var phoneWidth = 0;
  4. var timers = [];
  5. var timer ;
  6. Page({
  7.   data: {
  8.     imgUrls: [
  9.       'https://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
  10.       'https://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
  11.       'https://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
  12.     ],
  13.     indicatorDots: true,
  14.     autoplay: true,
  15.     interval: 3000,
  16.     duration: 500,
  17.     barrageTextColor:"#D3D3D3",
  18.     barrage_inputText:"none",
  19.     barrage_shoottextColor:"black",
  20.     bind_shootValue:"",
  21.     barrage_style:[],
  22.     barragefly_display:"none",
  23.   },
  24.  
  25.  
  26.     // 生命周期函数--监听页面加载
  27.   onLoad:function(options){
  28.     var that = this;
  29.     //获取屏幕的宽度
  30.       wx.getSystemInfo({
  31.         success: function(res) {
  32.            that.setData({
  33.                 barrage_phoneWidth:res.windowWidth-100,
  34.            })
  35.         }
  36.       })
  37.       phoneWidth = that.data.barrage_phoneWidth;
  38.       console.log(phoneWidth);
  39.   },
  40.  
  41.   //是否打开弹幕... 
  42.   barrageSwitch: function(e){
  43.     console.log(e);
  44.     //先判断没有打开
  45.     if(!e.detail.value){
  46.     //清空弹幕
  47.       barrage_style_arr = [];
  48.       //设置data的值
  49.       this.setData({
  50.         barrageTextColor:"#D3D3D3",
  51.         barrage_inputText:"none",
  52.         barragefly_display:"none",
  53.         barrage_style:barrage_style_arr,
  54.       });
  55.       //清除定时器
  56.       clearInterval(timer);
  57.     }else{
  58.       this.setData({
  59.         barrageTextColor:"#04BE02",
  60.         barrage_inputText:"flex",
  61.         barragefly_display:"block",
  62.       });
  63.       //打开定时器
  64.         timer= setInterval(this.barrageText_move,800)
  65.     }
  66.   },
  67.  
  68.   //发射按钮
  69.   shoot: function(e){
  70.  
  71.     //字体颜色随机
  72.     var textColor = "rgb("+parseInt(Math.random()*256)+","+parseInt(Math.random()*256)+","+parseInt(Math.random()*256)+")";
  73.     // //设置弹幕字体的水平位置样式
  74.     // var textWidth = -(this.data.bind_shootValue.length*0);
  75.     //设置弹幕字体的垂直位置样式
  76.     var barrageText_height = (Math.random())*266;
  77.      barrage_style_obj = {
  78.       // textWidth:textWidth,
  79.       barrageText_height:barrageText_height,
  80.       barrage_shootText:this.data.bind_shootValue,
  81.       barrage_shoottextColor : textColor,
  82.       barrage_phoneWidth:phoneWidth
  83.     };
  84.     barrage_style_arr.push(barrage_style_obj);
  85.     this.setData({ 
  86.       barrage_style:barrage_style_arr,        //发送弹幕
  87.       bind_shootValue:""                    //清空输入框
  88.     })
  89.  
  90.       //定时器  让弹幕动起来
  91.       //  this.timer= setInterval(this.barrageText_move,800);
  92.  
  93.   },
  94.  
  95. //定时器  让弹幕动起来
  96.   barrageText_move: function(){
  97.     var timerNum = barrage_style_arr.length;
  98.     var textMove ;
  99.     for(var i=0;i<timerNum;i++){
  100.        textMove = barrage_style_arr[i].barrage_phoneWidth;
  101.        console.log("barrage_style_arr["+i+"].barrage_phoneWidth----------:"+barrage_style_arr[i].barrage_phoneWidth);
  102.        textMove = textMove -20;
  103.       barrage_style_arr[i].barrage_phoneWidth = textMove;
  104.       //走完的移除掉
  105.       if(textMove<=-100){
  106. //         clearTimeout(this.timer);
  107.           barrage_style_arr.splice(0,1);
  108.           i--;
  109.           //全部弹幕运行完
  110.           if(barrage_style_arr.length==0){
  111.             this.setData({
  112.               barrage_style:barrage_style_arr, 
  113.             })
  114.             // clearInterval(this.timer);
  115.             return;
  116.           }
  117.       }
  118.       console.log("第"+i+"个定时器:",textMove);
  119.       this.setData({
  120.         barrage_style:barrage_style_arr, 
  121.       })
  122.     }
  123.  
  124.  
  125.   },
  126.  
  127.   //绑定发射输入框,将值传递给data里的bind_shootValue,发射的时候调用
  128.   bind_shoot:function(e){
  129.     this.setData({
  130.       bind_shootValue:e.detail.value
  131.     })
  132.   },
  133.  
  134. })


因为刚刚接触小程序,所以对一些语句的使用都不是很了解。所以遇到了一些问题:
1、在js中获取wxml的控件的信息。
  js:
  1. barrageSwitch: function(e){
  2.     console.log(e);
  3.   }
  wxml:
  1. <switch id="switch_" bindchange="barrageSwitch"/>

结果:返回了一个objec.在控制台返回的类型好像都是json格式的数据。
  1. Object {type: "change", timeStamp: 2766, target: Object, currentTarget: Object, detail: Object}

2、在实现弹幕的时候,点击发射按钮,如何获取到输入框的信息。
在这,我是输入框失去焦点的时候,将数据复制给js中的data类,再点击发射的时候取data类中的值。

3、其中最大的问题是如何让文字跑起来,因为小程序不支持jQuery,让我这个js白痴有点无能为力。
在这说说自己让文字移动的思路:
首先,在打开弹幕的时候定义一个定时器,关闭的弹幕的时候把定时器给关掉。因为我是用数组来存储文字移动的样式,其他确定下来。我只要改变left的大小就可以让文字移动。所以我用for循环,当定时器运行的时候改变弹幕文字样式 left:xxx px;的大小。




免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。