首页 > 编程技术 > js

uniapp五分钟实现刷抖音小程序教程示例

发布时间:2023-3-17 13:59 作者:蜡笔小心_

前言

最近闲来无事,在自己的小程序里面集成了一个小视频的接口,但是由于小程序对于播放视频的限制,只能用来做一个demo刷视频了,没办法上线体验。

小程序播放视频限制最多10个,超出可能就崩了。

我也有想过用js去追加和删减,但是还是有点麻烦了,等有空了再把想法化为现实吧。

演示一下看看

去掉小程序顶部栏

{
    "path": "pages/searchvideo/searchvideo",
    "style": {
        "navigationBarTitleText": "小视频",
        "enablePullDownRefresh": false,
        "navigationStyle": "custom"
    }
}

顶部栏可以根据自己的需求自定义,我这里放了一个类似于抖音的 tab栏

滚动小视频

uniapp和原生的微信小程序里面都有 swiper 标签用于做滚动或轮播效果的组件,所以我们可以直接利用这个组件做出我们想要的效果。

组件

<swiper class="card-swiper" :circular="true" vertical="true" :autoplay="true" duration="500"
        interval="5000" @change="cardSwiper">
    <swiper-item v-for="(item,index) in swiperList" :key="index" :class="cardCur==index?'cur':''">
        <view class="swiper-item image-banner">
            <video :id="`video-${item.id}`" :src="item.mp4" loop style="height: 100vh;width: 100vw;"></video>
        </view>
    </swiper-item>
</swiper>

数据

cardCur: 0,
swiperList: [{
    id: 0,
    mp4: 'http://vcdnb.huoying666.com/new_video/2022/0725/b94a235358c31668dc99e7cff9fe5e9c/v1080/b94a235351_6921661_fhd.mp4'
}, {
    id: 1,
    mp4: 'http://vcdnb.huoying666.com/new_video/2020/1211/9d0b01c88bd05721f9de88122de72db1/v1080/9d0b01c881_5872976_fhd.mp4'
}, {
    id: 2,
    mp4: 'http://vcdnb.huoying666.com/new_video/2021/1109/6f5610c304083ca59141c8f70aca6396/v720/6f5610c301_6578243_hd.mp4'
}]

样式

.card-swiper {
        height: 100vh !important;
}

.card-swiper swiper-item {
        width: 750rpx !important;
        left: 0rpx;
        box-sizing: border-box;
        overflow: initial;
}

.card-swiper swiper-item .swiper-item {
        width: 100%;
        display: block;
        height: 100vh;
        border-radius: 0rpx;
        transform: scale(1);
        transition: all 0.2s ease-in 0s;
        overflow: hidden;
}

.card-swiper swiper-item.cur .swiper-item {
        transform: none;
        transition: all 0.2s ease-in 0s;
}

.card-swiper swiper-item .swiper-item-png {
        margin-top: -50vh;
        width: 100%;
        display: block;
        border-radius: 0rpx;
        transform: translate(1040rpx, 20rpx) scale(0.5, 0.5);
        transition: all 0.6s ease 0s;
}

.card-swiper swiper-item.cur .swiper-item-png {
        margin-top: -100vh;
        width: 900rpx;
        transform: translate(-80rpx, 0rpx) scale(1, 1);
        transition: all 0.6s ease 0s;
}

.image-banner {
        display: flex;
        align-items: center;
        justify-content: center;
}

.image-banner image {
        width: 100%;
}

事件

cardSwiper(e) {
    this.cardCur = e.detail.current
    for (let i = 0; i < this.swiperList.length; i++) {
        const videoContext = uni.createVideoContext(`video-${this.swiperList[i]['id']}`, this)
        if (i === this.cardCur) {
            videoContext.play()
        } else {
            videoContext.stop()
        }
    }
}

由于uniapp是可以直接编译成 H5 的,所以我们就直接在掘金代码片段中看一下效果吧。

代码片段

代码片段里面的内容是直接引用的uniapp云发布的链接,如果想了解这一块的内容,可以单独开篇文章写一下的。

<div id="app">
  <iframe" width=100% src="https://static-54d8ac48-ba3d-4f0d-8a0b-029cbc34a4b3.bspapp.com/#/" width="400" height="800"></iframe>
</div>

 {"success":false,"error":{"code":"InvalidSpace.Deleted","message":"The space is already deleted."},"data":null}

写完了就得总结一下

这个功能对于小程序来说做起来不算太复杂,也是由于微信的限制,不能做出太复杂的刷视频的功能。大家可以根据自己的需求去修改这一块的代码

以上就是uniapp五分钟实现刷抖音小程序教程示例的详细内容,更多关于uniapp刷抖音小程序的资料请关注猪先飞其它相关文章!

原文出处:https://juejin.cn/post/7140232886413852685

标签:[!--infotagslink--]

您可能感兴趣的文章: