在其他设备上调用Jquery命令

时间:2019-04-05 13:20:58

标签: jquery swiper

在使用移动设备制作的轮播中,Swiper Demo出现了问题。在移动设备上时,滑动功能似乎无法按照图片的正确顺序移动。我想我搞砸了如何在每个设备上调用jquery。如果您想检查一下,请在此处加载事件轮播www.besocialscene.com

$(document).ready(function () {
var swiper = new Swiper('.swiper-container', {
      //should be for desktop/laptop//
      slidesPerView: 4,
      spaceBetween: 20,
      loop: true,
       navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      }
    });  

 if( $(window).width()<= 768){
  var swiper = new Swiper('.swiper-container', {   
     slidesPerView: 'auto',
     spaceBetween: 10,
     centeredSlides: true,
     loop: true,
     navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
     }

   });     
    } else if($(window).width() <= 1024){
        var swiper = new Swiper('.swiper-container', {   
         slidesPerView: 3,
         spaceBetween: 20,
         loop: true,
         navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
      }
     });



   }   

   $('[data-ctabtn]').on('click', function() {
    var idx = $(this).data('ctabtn');
    $('.event' + idx + 'back').show();
    $('.event' + idx + 'front').remove();
 });
}); 

1 个答案:

答案 0 :(得分:0)

这可以做到-但由于您的问题没有提供有效的代码段,因此我无法对其进行测试。

$( document ).ready( function () {

  var swiper;

  // setting up swiper for the first time on $(document).ready()
  initSwiper();

  // initializing swiper with general attributes
  function initSwiper() {

    swiper = new Swiper( '.swiper-container', {
      loop: true,
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev'
      }
    } );

    setSwiper();

  }

  // setting swiper attributes
  function setSwiper() {

    if ( $( window ).width() <= 768 ) {
      swiper.slidesPerView = 'auto';
      swiper.spaceBetween = 10;
      swiper.centeredSlides = true;
    } else if ( $( window ).width() <= 1024 ) {
      swiper.slidesPerView = 3;
      swiper.spaceBetween = 20;
      swiper.centeredSlides = false;
    } else {
      swiper.slidesPerView = 4;
      swiper.spaceBetween = 20;
      swiper.centeredSlides = false;
    }

  }

  // make the swiper responsive
  $( window ).resize( function() {

    setSwiper();

  } );

  $( '[data-ctabtn]' ).on( 'click', function() {
    var idx = $( this ).data( 'ctabtn' );
    $( '.event' + idx + 'back' ).show();
    $( '.event' + idx + 'front' ).remove();
  } );

});