微信小程式swiper元件實現抖音翻頁切換視訊功能的例項程式碼
阿新 • • 發佈:2020-06-25
微信小程式用swiper元件實現仿抖音短視訊上下划動整頁切換視訊功能demo
利用swiper元件可簡單快速編寫仿抖音短視訊的功能 自動播放當前頁視訊翻頁停止播放當前頁視訊 並自動播放下頁視訊
有其他需求也可用 cover-view 新增 收藏 點贊 評論等功能
效果圖:
video官方介紹:https://developers.weixin.qq.com/miniprogram/dev/component/video.html
swiper官方介紹:https://developers.weixin.qq.com/miniprogram/dev/component/swiper.html
注: 官方一篇文件介紹,基礎庫 2.4.4 以下版本,video (原生) 元件不支援在 swiper中使用
介紹:https://developers.weixin.qq.com/miniprogram/dev/component/native-component.html
臭豆腐 腐乳 加檸檬 上程式碼就完了奧利給 ! ! ! (裡面有自定義導航欄的程式碼但不多 參考著康康... 吼吼吼...)
wxml:
<!-- 自定義頭部導航欄 --> <cover-view style="height:{{statusBarHeight+navBarHeight}}px"> <navigator open-type="navigateBack"> <cover-image src="../../icon/返回.png" /> <!--填寫自己的圖示地址--> </navigator> </cover-view> <!--swiper實現整屏划動播放視訊--> <swiper vertical duration="200" bindchange="slide" style="height:{{screenHeight}}px; width:100%;background:#000"> <block wx:for="{{video}}" wx:key="id"> <swiper-item style="height:100%; width:100%"> <video wx:if="{{index==changeIndex}}" style="height:100%; width:100%" src="{{item.video}}" autoplay="true" /> </swiper-item> </block> </swiper> <!-- swiper中background:#000; 設定swiper背景色 否則翻頁的時候會有白影 影響效果 -->
wxss:
cover-view { width: 100%; position: fixed; z-index: 999; } cover-image { width: 17px; height: 17px; margin-left: 8px; padding-right: 20px; position: absolute; bottom: 11px; }
json:
{ "navigationBarTextStyle": "white","navigationStyle": "custom","usingComponents": {} }
js:
//獲取應用例項 const app = getApp() Page({ /** * 頁面的初始資料 */ data: { screenHeight: app.screenHeight,//獲取螢幕高度 statusBarHeight: app.statusBarHeight,//獲取狀態列高度 navBarHeight: app.navBarHeight,//獲取導航欄高度 changeIndex: 0,video: [{ id: 0,video: "/*視訊地址*/" },{ id: 1,{ id: 2,video: "/*視訊地址*/" }] },//划動切換 slide(e) { this.setData({ changeIndex: e.detail.current }) console.log(e.detail.current) } })
app.js
App({ onLaunch: function() { // 獲取系統資訊 wx.getSystemInfo({ success: (res) => { // 獲取螢幕高度 this.screenHeight = res.screenHeight // 獲取狀態列高度 this.statusBarHeight = res.statusBarHeight // 通過作業系統 確定自定義導航欄高度 if (res.system.substring(0,3) == "iOS") { this.navBarHeight = 42 } else { this.navBarHeight = 44 } } }) } })
總結
到此這篇關於微信小程式swiper元件實現抖音翻頁切換視訊功能的例項程式碼的文章就介紹到這了,更多相關微信小程式實現抖音翻頁切換視訊內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!