Video carousel in H5 page (similar to banner carousel effect)

Let me talk about my needs first, as shown in the figure below:
The middle part of the mobile phone model is for video playback. When a video is played, the entire screen will automatically slide up to switch to the next video.

renderings

For detailed effect display, please click on the PC side : http://www.17jianyue.cn/ to view

When it comes to carousel, what we see most is the banner carousel, and when it comes to carousel, I will immediately think of using Swiper.
At the beginning, for rapid development, I wanted to use swiper to achieve the effect of switching video rotation, but after trying, I found two serious problems:
1. When using Swiper to realize rotation, the set rotation time interval is a fixed value , and videos play for different lengths of time, so video rotation is based on video length.
2. If swiper is used, it is mandatory to set a fixed time interval to switch playback, which will cause the videos in the video list to play at the same time. In other words, just after entering the page, although the page shows that the first video is playing, all the videos are loading and playing at the same time in the background.
Therefore, swiper can be ignored.


Let's get to the point:

When the video is playing, when can I switch to the next one? When the video finishes playing. Therefore, you need to use the ended() event to monitor whether the playback is complete.
To achieve seamless rotation effect when rotating.

The code focuses on the js part:

html:

   <div class="video_list">
    <div class="video_ls"></div>
   </div>

js:

var cdnUrl = '';  //资源url

//视频列表数据,三个视频
var videoList = [
    {
        resid:'j1q9vb170b2769761317e270eccdfe778e0b46df.mp4',
        user_img:'user_xx.png',
        dialog_img:'dialog_xx.png',
        topic_img:'topic_xx.png'
    },
    {
        resid:'j1yvqkg00b2769761317e270eccdfe778e0b46df.mp4',
        user_img:'user_xyj.png',
        dialog_img:'dialog_xyj.png',
        topic_img:'topic_xyj.png'
    },
    {
        resid:'j2037nea006c4c1b8a8ac173362ee25ee7ec24c4.mp4',
        user_img:'user_3.png',
        dialog_img:'dialog_3.png',
        topic_img:'topic_3.png'
    },              

];  

var leg = videoList.length;   

$(document).ready(function(){
    //加载视频列表
    var str='';
    for(var i=0;i<leg;i++){
        loadvideo(videoList[i],i);  
    }

    //实现无缝滚动
    var clone = $(".video_ls .video_shows").first().clone(true);//克隆第一个视频
    $(".video_ls").append(clone);//复制到列表最后
    var size = $('.video_shows').length;

    //视频高度
    var _height = $('.video_shows').outerHeight();

    //初始化播放第一条
    var video_show = $('.video_shows');
    var video = $('.video_shows video');       
    video[0].load();     
    video[0].play(); 

     var num =0;  //当前播放视频的下标
     playlist(video);

     function playlist(video){
         //监控当前视频是否播放完毕
         video[num].onended = function(){
            //console.log("第"+(num+1)+"条视频播放完毕")
            num++;              
            if(num<video.length){
                var _top = -_height*(num)+'px';
                $('.video_ls').animate({'top':_top},'1500')                            
            }else{
                    num=1;
                    $(".video_ls").css('top','0');
                    $('.video_ls').animate({'top':-_height},'1500')                            
            }

            video[num].load();     
            video[num].play(); 
            return playlist(video); 
        }               

    }           

})


//加载视频播放界面
function loadvideo(videodta,i){
   str = '';
   str += '<div class="video_shows">';
   str += '<div class="userInfo"><img src="image/'+videodta.user_img+'"/></div>';              
   str += '<div class="dialog"><img src="image/'+videodta.dialog_img+'"/></div>';
   str += '<div class="more"></div>';
   str += '<div class="topic_content"><img src="image/'+videodta.topic_img+'"/></div>';
   str += '<div class="phone_btm"></div>';
   str += '<video id="myvideo'+(i+1)+'" class="video-js vjs-big-play-centered"  poster="'+cdnUrl+videodta.resid+'?vframe/jpg/offset/0"   preload="auto">';
   str += '<source src="'+cdnUrl+videodta.resid+'" type="video/mp4">';
   str += '</video>';
   str += '</div>';
   $(".video_ls").append(str);
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324521442&siteId=291194637