本文实例为大家分享了微信小程序实现弹幕墙的具体代码,供大家参考,具体内容如下
为了解决左右弹幕重叠问题,也是找了网上挺多案例,最后都不行,最后利用四个数组和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 })
为大家推荐现在关注度比较高的微信小程序教程一篇:《微信小程序开发教程》小编为大家精心整理的,希望喜欢。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。
- 本文固定链接: https://zxbcw.cn/post/200035/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)