微信小程序實現抖音播放效果的實例代碼
最近項目要做一個類似于抖音的一個視頻播放 需要小程序完成
在再次確定了需要這個需求的情況下就開始了(其實因為不是說這個功能不好做主要是但心做出來肯定不流暢 卡頓什么的 優(yōu)化不好優(yōu)化)
然后就開始啦
思路使用微信的 swiper 完成豎向滑動 然后分頁加載首先先加載一次加載10個當滑動到第7個的時候加載下一頁 (要處理自動播放的問題和加載多個有多個同時播放的問題)
效果圖
代碼
<swiper class="swiper" vertical='true' easing-function='default' bindchange='bindchange'> <block wx:for="{{weishipinglist}}" wx:for-item="key" wx:key="*this"> <swiper-item> <view class="video-wrap"> <video wx:if="{{index==hkindex}}" class="video" autoplay="{{true}}" lopp='{{true}}' src="{{key.videoid}}" enable-play-gesture='{{true}}' show-fullscreen-btn='{{false}}' controls='{{false}}' vslide-gesture-in-fullscreen='{{false}}' show-play-btn='{{false}}' enable-progress-gesture='{{false}}' show-mute-btn='{{false}}'></video> <button class="buy" bindtap="click">{{index}}</button> </view> </swiper-item> </block> </swiper>
data: { weishipinglist: [],//視頻數據 hkindex: 0,//滑塊index }, //動態(tài)更新當前滑塊下標 bindchange(e) { this.setData({ hkindex: e.detail.current }) if (e.detail.current%10 == 7) { this.chaxunzhi(); //此處是表示在快要加載完了需在分頁請求加載 } },
//css代碼可能有多余的 我就不挑了 引入時自己按需引入吧 page { width: 100%; height: 100%; } .video-wrap { width: 100%; height: 100%; position: relative; /* border: 1px dashed red; */ } .video-wrap video { width: 100%; height: 100%; position: absolute; top: 0px; left: 0; z-index: 1; } .video-wrap .buy { width: 100rpx; height: 100rpx; line-height: 100rpx; border-radius: 50%; position: absolute; z-index: 100; bottom: 100rpx; left: 50rpx; font-size: 11pt; text-align: center; padding: 0px; } .swiper { width: 100%; height: 100%; } .tentbiaot { width: 400rpx; font-size: 30rpx; color: #fff; z-index: 99; white-space: normal; line-height: 40rpx; margin-top: 20rpx; } .diwen { width: 400rpx; color: #fff; z-index: 99; display: flex; align-items: center; } .toixaign { width: 50rpx; height: 50rpx; border-radius: 50rpx; margin-right: 10rpx; } .teiename { font-size: 34rpx; margin-right: 10rpx; } .diwe { display: flex; position: fixed; bottom: 100rpx; flex-direction: column; z-index: 99; left: 40rpx; } .dianzaidijila { width: 100rpx; position: fixed; right: 30rpx; bottom: 80rpx; display: flex; flex-direction: column; align-items: center; justify-content: center; z-index: 99; } .tuaobiao { display: flex; align-items: center; flex-direction: column; justify-content: center; margin-bottom: 30rpx; } .tobimg{ width: 60rpx; height: 50rpx; } .tobimglw{ width: 60rpx; height: 60rpx; margin-bottom: 20rpx; } .zitiet{ color: #fff; font-size: 26rpx; margin-top: 6rpx; } .zhaunfanan { width: 60rpx; height: 50rpx; padding: 0rpx; border: none !important; line-height: 0rpx; } .zhaunfananas { width: 60rpx; height: 50rpx; }
完了需要注意的就是一個分頁加載 我設置的是7因為我們數據是一頁10條 會在第7條加載第二頁數據
wx:if="{{index==hkindex}}" 這個起到的是控制加載要不會多個同時播放沒刷到的也會播放 現在只會播放當前頁面視頻
總結
到此這篇關于微信小程序實現抖音播放效果的實例代碼的文章就介紹到這了,更多相關微信小程序抖音播放內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
javascript設計模式 – 組合模式原理與應用實例分析
這篇文章主要介紹了javascript設計模式 – 組合模式,結合實例形式分析了javascript組合模式相關概念、原理、應用場景及相關使用注意事項,需要的朋友可以參考下2020-04-04JavaScript利用split函數按規(guī)定截取字符串(獲取郵箱用戶名)
這個其實就是利用了js的split函數,以@分割數組,一般用這個的地方不多,但這個思路應用的比較廣泛。推薦大家學習。2009-12-12