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

编程学习 2021-07-04 14:07www.dzhlxh.cn编程入门
这篇文章主要为大家详细介绍了微信小程序实现弹幕墙、祝福墙效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

为了解决左右弹幕重叠问题,也是找了网上挺多案例,最后都不行,最后利用四个数组和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
})

为大家推荐现在关注度比较高的微信小程序教程一篇:长沙网络推广为大家精心整理的,希望喜欢。

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

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板