欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  互联网

微信小程序实现弹幕墙(祝福墙)

程序员文章站 2022-08-27 13:10:16
本文实例为大家分享了微信小程序实现弹幕墙的具体代码,供大家参考,具体内容如下为了解决左右弹幕重叠问题,也是找了网上挺多案例,最后都不行,最后利用四个数组和css3动画animation来解决这是个祝福...

本文实例为大家分享了微信小程序实现弹幕墙的具体代码,供大家参考,具体内容如下

为了解决左右弹幕重叠问题,也是找了网上挺多案例,最后都不行,最后利用四个数组和css3动画animation来解决
这是个祝福墙,用户发送弹幕到后台审核,审核通过显示在前端

微信小程序实现弹幕墙(祝福墙)

<!-- 弹幕墙 -->
<view class="barrage" wx:if="{{ispush}}">
 <view class="barrage-a">
 <view class="barrage-b"><image src="../../images/img/a-13.png"></image></view>
 <view class="barrage-c">
 <view class="video_container">
 <view class='danmu'>
 <view class="danmu_wrapper">
 <view class="li0" wx:if="{{danmufake0.cnt}}" style="width:{{danmufake0.max}}rpx; animation: dmanimation {{danmufake0.max / 150}}s linear 1s infinite backwards;">
 <view wx:for="{{danmufake0.cnt}}" wx:key="danmufake0cntindex" data-idx="{{item.idx}}" data-width="{{item.width}}" class='danmu-li rowandcolcenter' style="left:{{item.left}}rpx">
 <text>{{item.title}}</text>
 </view>
 </view>
 <view class="li1" wx:if="{{danmufake1.cnt}}" style="width:{{danmufake1.max}}rpx; animation: dmanimation {{danmufake1.max / 150}}s linear 3s infinite backwards;">
 <view wx:for="{{danmufake1.cnt}}" wx:key="danmufake1cnt" data-idx="{{item.idx}}" data-width="{{item.width}}" class='danmu-li rowandcolcenter' style="left:{{item.left}}rpx">
 <text>{{item.title}}</text>
 </view>
 </view>
 <view class="li2" wx:if="{{danmufake2.cnt}}" style="width:{{danmufake2.max}}rpx; animation: dmanimation {{danmufake2.max / 150}}s linear 5s infinite backwards;">
 <view wx:for="{{danmufake2.cnt}}" wx:key="danmufake2cnt" data-idx="{{item.idx}}" data-width="{{item.width}}" class='danmu-li rowandcolcenter' style="left:{{item.left}}rpx">
 <text>{{item.title}}</text>
 </view>
 </view>
 <view class="li3" wx:if="{{danmufake3.cnt}}" style="width:{{danmufake3.max}}rpx; animation: dmanimation {{danmufake3.max / 150}}s linear 7s infinite backwards;">
 <view wx:for="{{danmufake3.cnt}}" wx:key="danmufake3cnt" data-idx="{{item.idx}}" data-width="{{item.width}}" class='danmu-li rowandcolcenter' style="left:{{item.left}}rpx">
 <text>{{item.title}}</text>
 </view>
 </view>

 </view>
 </view>
 </view>
 <form>
 <view class="danmugrop">
 <input type="text" name="name" bindinput="getinput" class="danmuinput" placeholder='请输入你的祝福语' placeholder-style="color:#b6b2a6;opacity:.55;"></input>
 <button style="width:108rpx;" class="senddanmu" bindtap='senddanmu' form-type='reset'>
 <image src="/images/img/a-3.png"></image>
 <text>发送</text>
 </button>
 </view>
 </form>
 </view>
 </view>
</view>

js:

var danmu0=[];
var danmu1=[];
var danmu2=[];
var danmu3=[];
 for (var i = 0; i < res.data.entity.length; i++) {
 const num = math.floor(math.random() * 4);
 if(num == 0){
 var left = 0;
 if(danmu0.length){left = danmu0[danmu0.length-1].left + danmu0[danmu0.length-1].width + 80;}
 left = left < 0 ? 0 : left;
 danmu0.push({
 title: res.data.entity[i].newscontent,
 left: left,
 width:res.data.entity[i].newscontent.length*24,
 idx: num
 });
 }
 if(num == 1){
 var left = 0;
 if(danmu1.length){left = danmu1[danmu1.length-1].left + danmu1[danmu1.length-1].width + 80;}
 left = left < 0 ? 0 : left;
 danmu1.push({
 title: res.data.entity[i].newscontent,
 width:res.data.entity[i].newscontent.length*24,
 left: left,
 idx: num
 });
 }
 if(num == 2){
 var left = 0;
 if(danmu2.length){left = danmu2[danmu2.length-1].left + danmu2[danmu2.length-1].width + 80;}
 left = left < 0 ? 0 : left;
 danmu2.push({
 title: res.data.entity[i].newscontent,
 width:res.data.entity[i].newscontent.length*24,
 left: left,
 idx: num
 });
 }
 if(num == 3){
 var left = 0;
 if(danmu3.length){left = danmu3[danmu3.length-1].left + danmu3[danmu3.length-1].width + 80;}
 left = left < 0 ? 0 : left;
 danmu3.push({
 title: res.data.entity[i].newscontent,
 width:res.data.entity[i].newscontent.length*24,
 left: left,
 idx: num
 });
 }
 }
 that.setdata({
 danmufake0: {
 max : danmu0[danmu0.length-1].left + danmu0[danmu0.length-1].width + 80,
 cnt:danmu0
 },
 danmufake1: {
 max : danmu1[danmu1.length-1].left + danmu1[danmu1.length-1].width + 80,
 cnt:danmu1
 },
 danmufake2: {
 max : danmu2[danmu2.length-1].left + danmu2[danmu2.length-1].width + 80,
 cnt:danmu2
 },
 danmufake3: {
 max : danmu3[danmu3.length-1].left + danmu3[danmu3.length-1].width + 80,
 cnt:danmu3
 }
 })

 var danmumaxwid = math.max(that.data.danmufake0.max,that.data.danmufake1.max,that.data.danmufake2.max,that.data.danmufake3.max);
 that.setdata({
 danmumaxwid:danmumaxwid
})

为大家推荐现在关注度比较高的微信小程序教程一篇:《微信小程序开发教程》小编为大家精心整理的,希望喜欢。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。