您好,登錄后才能下訂單哦!
小程序開發中如何實現一個彈幕墻功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
具體內容如下
<!-- 彈幕墻 --> <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}}" > <view wx:for="{{danmuFake0.cnt}}" wx:key="danmuFake0cntindex" data-idx="{{item.idx}}" data-width="{{item.width}}" class='danmu-li rowAndColCenter' > <text>{{item.title}}</text> </view> </view> <view class="li1" wx:if="{{danmuFake1.cnt}}" > <view wx:for="{{danmuFake1.cnt}}" wx:key="danmuFake1cnt" data-idx="{{item.idx}}" data-width="{{item.width}}" class='danmu-li rowAndColCenter' > <text>{{item.title}}</text> </view> </view> <view class="li2" wx:if="{{danmuFake2.cnt}}" > <view wx:for="{{danmuFake2.cnt}}" wx:key="danmuFake2cnt" data-idx="{{item.idx}}" data-width="{{item.width}}" class='danmu-li rowAndColCenter' > <text>{{item.title}}</text> </view> </view> <view class="li3" wx:if="{{danmuFake3.cnt}}" > <view wx:for="{{danmuFake3.cnt}}" wx:key="danmuFake3cnt" data-idx="{{item.idx}}" data-width="{{item.width}}" class='danmu-li rowAndColCenter' > <text>{{item.title}}</text> </view> </view> </view> </view> </view> <form> <view class="danmuGrop"> <input type="text" name="Name" bindinput="getInput" class="danmuinput" placeholder='請輸入你的祝福語' placeholder-></input> <button 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 })
關于小程序開發中如何實現一個彈幕墻功能問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。