本文實(shí)例為大家分享了videojs+swiper實(shí)現(xiàn)淘寶商品詳情輪播圖的具體代碼,供大家參考,具體內(nèi)容如下
這個(gè)引用了videojs和swiper。實(shí)現(xiàn)效果類似淘寶商品詳情中的輪播圖,首張輪播為視頻:
當(dāng)視頻開始播放時(shí),輪播停止。視頻暫停時(shí),輪播繼續(xù)。
當(dāng)視頻播放中,滑動(dòng)到下個(gè)slide時(shí),視頻暫停播放。
HTML部分:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<!-- swiper輪播 --> < div class = "swiper-container" > < div class = "swiper-wrapper" > < div class = "swiper-slide" > < video id = "video" style = "width: 100%;height: 100%;" controls preload = "none" poster = "xxxx" class = "video-js vjs-big-play-centered" > < source src = "xxxx" type = "video/mp4" > </ video > </ div > < div class = "swiper-slide" >< img src = "xxxx" alt = "" ></ div > < div class = "swiper-slide" >< img src = "xxxx" alt = "" ></ div > < div class = "swiper-slide" >< img src = "xxxx" alt = "" ></ div > < div class = "swiper-slide" >< img src = "xxxx" alt = "" ></ div > </ div > <!-- 如果需要分頁(yè)器 --> < div class = "swiper-pagination" ></ div > </ div > |
js部分:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
//新建videojs對(duì)象 var player = videojs( 'video' , { muted: true , controls: true , // loop: true, }); // swiper輪播 var mySwiper = new Swiper ( '.swiper-container' , { direction: 'horizontal' , // 輪播圖的方向,也可以是vertical方向 loop: true , //循環(huán)播放 autoplay:3000, // slide自動(dòng)切換時(shí)間 speed:2000, // slide滑動(dòng)動(dòng)畫時(shí)間 height: 100, pagination: '.swiper-pagination' , // 如果需要分頁(yè)器,即下面的小圓點(diǎn) autoplayDisableOnInteraction : false , // 這樣,即使我們滑動(dòng)之后, 定時(shí)器也不會(huì)被清除 offsetWidth: 0, observer: true , //監(jiān)聽 // swiper從一個(gè)slide過(guò)渡到另一個(gè)slide時(shí)執(zhí)行:停止視頻播放(這里是swiper3,swiper4的寫法不同) onSlideChangeStart: function (swiper){ player.pause(); } }); //視頻播放時(shí)輪播圖停止 player.on( 'play' , function (){ // console.log(mySwiper) mySwiper.stopAutoplay() }); // 視頻暫停時(shí)輪播圖繼續(xù) player.on( 'pause' , function (){ mySwiper.startAutoplay() }); |
這里沒有引入swiper和videojs的js和css,可自行百度。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/weixin_42085115/article/details/90642127