uniapp实现app-nvue的快手抖音的视频上下滑动功能

uniapp-nvue页面文件:localVideo.nvue

<script>
    export default {
        data() {
            return {
                // 视频数据
                videoArr: [],
                // 显示的视频数据
                videoArrScroll: [],
                // 视频显示的开始下标
                startIndex: 0,
                // 视频开始的结束下标
                endIndex: 3,
                // 暂停播放
                playBob: true,
            }
        },
        onLoad() {
            let videoArr = uni.getStorageSync("videoArr")
            if (videoArr) {
                this.videoArr = JSON.parse(videoArr)
                this.videoArrScroll = this.videoArr.slice(0, 3)
                console.log("视频数据:", this.videoArr);
            }
        },
        onReady() {
            let videoArr = uni.getStorageSync("videoArr")
            if (videoArr) {
                const that = this
                const videoContent = uni.createVideoContext(that.videoArrScroll[0].id)
                videoContent.play()
            }
        },
        methods: {
            // 上传视频
            uploadVideos() {
                const that = this
                uni.chooseVideo({
                    sourceType: ['album'],
                    success: function(res) {
                        let obj = {
                            // 视频地址
                            src: res.tempFilePath,
                            // 视频描述
                            dsc: ''
                        }
                        obj.id = (new Date().getTime()) + ''
                        that.videoArr.unshift(obj)
                        that.startIndex = 0
                        that.endIndex = 3
                        that.videoArrScroll = that.videoArr.slice(0, 3)
                        setTimeout(() => {
                            let videoContents = null
                            that.videoArrScroll.forEach((item, index) => {
                                videoContents = uni.createVideoContext(item.id)
                                videoContents.pause()
                            })
                            videoContents = uni.createVideoContext(that.videoArrScroll[0].id)
                            videoContents.play()
                        }, 100)
                        uni.setStorageSync("videoArr", JSON.stringify(that.videoArr))
                    }
                });
            },
            // 滑动视频时触发
            videoChange(e) {
                const that = this
                const index = e.detail.current
                console.log("当前视频下标:", index);
                let videoContents = null
                that.videoArrScroll.forEach((item, index) => {
                    videoContents = uni.createVideoContext(item.id)
                    videoContents.pause()
                })
                console.log("当前视频ID:", that.videoArrScroll[index].id);
                videoContents = uni.createVideoContext(that.videoArrScroll[index].id)
                videoContents.play()
​
                that.endIndex += 1
                that.videoArrScroll = that.videoArr.slice(that.startIndex, that.endIndex)
            },
            // 实现数组乱序
            shuffleArray(array) {
                for (let i = array.length - 1; i > 0; i--) {
                    const j = Math.floor(Math.random() * (i + 1));
                    [array[i], array[j]] = [array[j], array[i]];
                }
                return array;
            },
            // 刷新
            refresh() {
                console.log("触发");
                const that = this
                const arr = JSON.parse(JSON.stringify(that.videoArr))
                that.videoArr = this.shuffleArray(arr);
                console.log("乱序的数组:", that.videoArr);
                that.startIndex = 0
                that.endIndex = 3
                that.videoArrScroll = that.videoArr.slice(0, 3)
                setTimeout(() => {
                    let videoContents = null
                    that.videoArrScroll.forEach((item, index) => {
                        videoContents = uni.createVideoContext(item.id)
                        videoContents.pause()
                    })
                    videoContents = uni.createVideoContext(that.videoArrScroll[0].id)
                    videoContents.play()
                }, 100)
                uni.setStorageSync("videoArr", JSON.stringify(that.videoArr))
            },
            // 单击视频播放暂停
            playVideo(videoId) {
                const that = this
                let videoContents = null
                that.videoArrScroll.forEach((item, index) => {
                    videoContents = uni.createVideoContext(item.id)
                    videoContents.pause()
                })
​
                let videoContent = null
                if (this.playBob) {
                    videoContent = uni.createVideoContext(videoId)
                    videoContent.pause()
                    this.playBob = false
                } else {
                    videoContent = uni.createVideoContext(videoId)
                    videoContent.play()
                    this.playBob = true
                }
​
            }
        }
    }
</script>
​
<template>
    <view class="box">
        <!-- 数据为空 -->
        <view class="dataNull" v-if="videoArrScroll.length==0">
            <u-empty mode="data" icon="../../static/dataNull.png">
            </u-empty>
        </view>
​
        <!--视频区域 
        vertical:设置轮播方向为竖向
        acceleration:实现快手视频滑动效果:当开启时,会根据滑动速度,连续滑动多屏
        -->
        <swiper v-if="videoArrScroll.length!=0" class="video_box" @change="videoChange" :vertical="true"
            :acceleration="true">
            <swiper-item v-for="item in videoArrScroll" :key="item.id" class="video_item">
                <video @click="playVideo(item.id)" style="width: 750rpx;height: 1334rpx;" :id="item.id" :src="item.src"
                    :loop='true' :controls="true"></video>
            </swiper-item>
        </swiper>
​
​
        <!-- 底部功能 -->
        <view class="bottom">
            <view class="bottom_item" @click="refresh">
                <image style="width: 70rpx;height: 70rpx;" src="../../static/shuaXing.png" mode=""></image>
                <text style="font-size: 30rpx;margin-top: 10rpx;">刷新一下</text>
            </view>
            <view class="bottom_item" @click="uploadVideos">
                <image style="width: 70rpx;height: 70rpx;" src="../../static/shangChuan.png" mode=""></image>
                <text style="font-size: 30rpx;margin-top: 10rpx;">上传本地视频</text>
            </view>
        </view>
    </view>
</template>
​
​
<style scoped>
    .dataNull {
        width: 750rpx;
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 400rpx;
    }
​
    .video_item {
        display: flex;
        justify-content: center;
        align-items: center;
    }
​
    .video_box {
        background-color: black;
        width: 750rpx;
        /* #ifdef APP-VUE */
        height: calc(100vh - 140rpx);
        /* #endif  */
​
        /* #ifdef APP-NVUE */
        flex: 1;
        margin-bottom: 140rpx;
        /* #endif  */
    }
​
    .box {
        /* #ifdef APP-VUE */
        height: 100vh;
        /* #endif  */
        /* #ifdef APP-NVUE */
        flex: 1;
        /* #endif  */
        background-color: #f2f2f2;
    }
​
    .bottom_item {
        display: flex;
        flex-direction: column;
        align-items: center;
    }
​
    .bottom {
        position: fixed;
        bottom: 0;
        z-index: 99999;
        width: 750rpx;
        height: 140rpx;
        background-color: white;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-around;
    }
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现仿视频滑动效果,你可以使用uni-app框架中的swiper组件结合相关的动画效果来实现。下面是一种可能的实现方式: 1. 在你的uni-app项目中,使用swiper组件创建一个滑动容器,设置为横向滑动。 ```html <swiper class="swiper" :autoplay="false" :indicator-dots="false" :circular="true"> <swiper-item v-for="(item, index) in videoList" :key="index"> <video :src="item.url" autoplay muted loop></video> </swiper-item> </swiper> ``` 2. 使用css样式来设置容器的布局和样式。 ```css .swiper { width: 100%; height: 100%; overflow: hidden; } .swiper-item { width: 100%; height: 100%; } video { width: 100%; height: 100%; object-fit: cover; } ``` 3. 使用JavaScript或者Vue的生命周期钩子函数来监听滑动事件,并根据滑动的距离和方向来实现动画效果。 ```javascript export default { data() { return { videoList: [...], // 视频列表数据 startX: 0, // 触摸起始点的X坐标 startY: 0, // 触摸起始点的Y坐标 moveX: 0, // 触摸移动中的X坐标 moveY: 0, // 触摸移动中的Y坐标 direction: '', // 滑动方向 currentIndex: 0 // 当前显示的视频索引 }; }, methods: { onTouchStart(e) { this.startX = e.changedTouches[0].pageX; this.startY = e.changedTouches[0].pageY; }, onTouchMove(e) { this.moveX = e.changedTouches[0].pageX; this.moveY = e.changedTouches[0].pageY; const offsetX = this.moveX - this.startX; const offsetY = this.moveY - this.startY; if (Math.abs(offsetY) < Math.abs(offsetX)) { // 水平滑动 if (offsetX > 0) { this.direction = 'right'; } else { this.direction = 'left'; } } else { // 垂直滑动 if (offsetY > 0) { this.direction = 'down'; } else { this.direction = 'up'; } } }, onTouchEnd() { if (this.direction === 'left') { // 向左滑动,切换到下一个视频 this.currentIndex++; if (this.currentIndex >= this.videoList.length) { this.currentIndex = 0; } } else if (this.direction === 'right') { // 向右滑动,切换到上一个视频 this.currentIndex--; if (this.currentIndex < 0) { this.currentIndex = this.videoList.length - 1; } } // 根据currentIndex更新swiper组件的activeIndex属性,实现视图切换 this.$refs.swiper.swiperRef.setActiveItem(this.currentIndex); } } }; ``` 上述代码中,通过监听触摸事件,根据滑动方向切换到对应的视频,并将视图更新到当前的视频。 这是一种简单的实现方式,你可以根据自己的需求进行扩展和优化。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值