本文實例為大家分享了videojs+swiper實現淘寶商品詳情輪播圖的具體代碼,供大家參考,具體內容如下
這個引用了videojs和swiper。實現效果類似淘寶商品詳情中的輪播圖,首張輪播為視頻:
當視頻開始播放時,輪播停止。視頻暫停時,輪播繼續。
當視頻播放中,滑動到下個slide時,視頻暫停播放。
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 > <!-- 如果需要分頁器 --> < 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對象 var player = videojs( 'video' , { muted: true , controls: true , // loop: true, }); // swiper輪播 var mySwiper = new Swiper ( '.swiper-container' , { direction: 'horizontal' , // 輪播圖的方向,也可以是vertical方向 loop: true , //循環播放 autoplay:3000, // slide自動切換時間 speed:2000, // slide滑動動畫時間 height: 100, pagination: '.swiper-pagination' , // 如果需要分頁器,即下面的小圓點 autoplayDisableOnInteraction : false , // 這樣,即使我們滑動之后, 定時器也不會被清除 offsetWidth: 0, observer: true , //監聽 // swiper從一個slide過渡到另一個slide時執行:停止視頻播放(這里是swiper3,swiper4的寫法不同) onSlideChangeStart: function (swiper){ player.pause(); } }); //視頻播放時輪播圖停止 player.on( 'play' , function (){ // console.log(mySwiper) mySwiper.stopAutoplay() }); // 視頻暫停時輪播圖繼續 player.on( 'pause' , function (){ mySwiper.startAutoplay() }); |
這里沒有引入swiper和videojs的js和css,可自行百度。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_42085115/article/details/90642127