变态另类在线观看资源网站-欧美在线一区视频播放-中文字幕国产一区在线播放-成人av传媒在线观看

284292114@qq.com 中國站
行業(yè)新聞 網(wǎng)站建設(shè) 網(wǎng)絡(luò)推廣 首頁>新聞中心>網(wǎng)站建設(shè)

jq實(shí)時監(jiān)測窗口大小,來調(diào)用不用的swiper效果

時間:2023-07-02   訪問量:0

在網(wǎng)站開發(fā)過程中,我們用得很多的插件之一就是swiper,但是我們在做左右多個slider切換的時候,當(dāng)換到手機(jī)端上的時候,我們需要切換顯示的個數(shù)。如果下圖:

比如PC端一行顯示4個圖片:

JS:

 var swiper = new Swiper('.swiper-container', {
              slidesPerView: 4,
              spaceBetween: 30,
              loop: true,
               autoplay:true,
                speed: 500,
                // 如果需要分頁器
            pagination: {
              el: '.swiper-pagination',
              clickable :true,
            },
            
            // 如果需要前進(jìn)后退按鈕
            navigation: {
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev',
            },
            });

image.png

但是顯示在手機(jī)上不能顯示4個,需要顯示1個或者兩個。

    var swiper = new Swiper('.swiper-container', {
                slidesPerView: 1,
                spaceBetween: 30,
                loop: true,
                 autoplay:true,
                  speed: 500,
                  // 如果需要分頁器
              pagination: {
                el: '.swiper-pagination',
                clickable :true,
              },
              
              // 如果需要前進(jìn)后退按鈕
              navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
              },
              });

image.png


那么我們就可以通過JS來監(jiān)聽窗口的大小變化來調(diào)用不同的代碼。

代碼如下:

 //打開網(wǎng)頁時的寬度,必須保留
var windowsize = $(window).width();
  if( windowsize > 768 ) {             //  屏寬1330觸發(fā)
              //主體
           var swiper = new Swiper('.swiper-container', {
              slidesPerView: 4,
              spaceBetween: 30,
              loop: true,
               autoplay:true,
                speed: 500,
                // 如果需要分頁器
            pagination: {
              el: '.swiper-pagination',
              clickable :true,
            },
            
            // 如果需要前進(jìn)后退按鈕
            navigation: {
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev',
            },
            });
           //主體
           
      }else{
           
            //主體        
          var swiper = new Swiper('.swiper-container', {
                slidesPerView: 1,
                spaceBetween: 30,
                loop: true,
                 autoplay:true,
                  speed: 500,
                  // 如果需要分頁器
              pagination: {
                el: '.swiper-pagination',
                clickable :true,
              },
              
              // 如果需要前進(jìn)后退按鈕
              navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
              },
              });
            //主體

      }



  //監(jiān)測網(wǎng)頁時的寬度變化,以及實(shí)時更新     
$(window).resize(function() {
  windowsize = $(window).width();
      if( windowsize > 768 ) {             //  屏寬1330觸發(fā)
              //主體
           var swiper = new Swiper('.swiper-container', {
              slidesPerView: 4,
              spaceBetween: 30,
              loop: true,
               autoplay:true,
                speed: 500,
                // 如果需要分頁器
            pagination: {
              el: '.swiper-pagination',
              clickable :true,
            },
            
            // 如果需要前進(jìn)后退按鈕
            navigation: {
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev',
            },
            });
           //主體
           
      }else{
           
            //主體        
          var swiper = new Swiper('.swiper-container', {
                slidesPerView: 1,
                spaceBetween: 30,
                loop: true,
                 autoplay:true,
                  speed: 500,
                  // 如果需要分頁器
              pagination: {
                el: '.swiper-pagination',
                clickable :true,
              },
              
              // 如果需要前進(jìn)后退按鈕
              navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
              },
              });
            //主體

      }
  });



以上是用JS來寫的?,F(xiàn)在提供另一方法 就是swiper自帶的效果:

 var swiper = new Swiper('.swiper-container', {
              slidesPerView: 4,
              spaceBetween: 30,
              loop: true,
               autoplay:true,
                speed: 500,

                // 如果需要分頁器
            pagination: {
              el: '.swiper-pagination',
              clickable :true,
            },
            
            // 如果需要前進(jìn)后退按鈕
            navigation: {
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev',
            },
            
            //響應(yīng)式個數(shù)          
            breakpoints: {               
            //當(dāng)寬度小于等于640時顯示
              640: { 
                slidesPerView: 1,
                spaceBetween: 20
               },
            //當(dāng)寬度小于等于768時顯示
              768: {
                slidesPerView: 3,
                spaceBetween: 30
               },
            //當(dāng)寬度小于等于992時顯示
              992: {
                slidesPerView: 4,
                spaceBetween: 30
               }   
            }  

            });



服務(wù)咨詢
1對1咨詢,專業(yè)客服為您解疑答惑
聯(lián)系銷售
15899750475
在線咨詢
聯(lián)系在線客服,為您解答所有的疑問
ARE YOU INTERESTED IN ?
感興趣嗎?

有關(guān)我們服務(wù)的更多信息,請聯(lián)系項(xiàng)目經(jīng)理

15899750475 楊先生

永吉县| 明水县| 佛坪县| 新竹县| 谢通门县| 巴中市| 金乡县| 平和县| 临洮县| 临武县| 北海市| 赤城县| 儋州市| 青川县| 太白县| 彰化县| 塘沽区| 清徐县| 阿尔山市| 珲春市| 独山县| 岚皋县| 岚皋县| 威海市| 沂源县| 辉南县| 深泽县| 兴宁市| 新郑市| 西平县| 黑山县| 永登县| 大庆市| 金坛市| 西充县| 石景山区| 成安县| 郯城县| 清镇市| 长兴县| 岳阳市|